Exploring JSON API Data with Vue Js

While working on my Vue Js project, I encountered an issue with displaying data from an API in JSON format. Despite using

this.obj =JSON.stringify(this.Flats);
and successfully seeing all the data using console.log, I struggled to loop over and view the payment object.

https://i.sstatic.net/J4Giz.png

BuildingsService.getAllFlats().then((response) => {
            this.Flats = response.data.response;

       
             this.obj =JSON.stringify(this.Flats);
             console.log(this.Flats,"dataaa")

           
        });
  <div v-for="(object,index) in obj" :key="index"> //didn't work 
                               <span> {{object.flat_number}}   </span>

                                      <span > {{object.payment}}   </span>
                                </div>

Answer №1

It seems like you're fetching the array in Flats, converting it to a string with JSON.stringify, and then storing the result in the variable obj. Subsequently, you are trying to iterate through the string as if it were an array.

Consider this revised approach:

BuildingsService.getAllFlats().then((response) => {
    this.Flats = response.data.response;
    this.obj = this.Flats; // Keeping a duplicate string variable may be unnecessary
});

Alternatively, simply loop through the existing Array you have stored:

<div v-for="(object,index) in Flats" :key="index"> // This method should work fine

Answer №2

Unable to add comments, but it seems like you are encountering an error related to accessing a property called payment.

The error is likely due to some objects in the array not containing the payment object.

Implementing v-if="object.payment" should help resolve this issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to dynamically replace a section of a link with the current URL using JavaScript or jQuery?

I have a link that appears on multiple pages, and I want to dynamically change part of the link based on the current URL* of the page being visited. (*current URL refers to the web address shown in the browser's address bar) How can I use JavaScript ...

Waiting for Selenium in Javascript

I'm having trouble using Selenium Webdriver with Node.js to scrape Google Finance pages. The driver.wait function is not behaving as expected. I've configured my Mocha timeout at 10 seconds and the driver.wait timeout at 9 seconds. The test passe ...

Guide to displaying numerous points on mapbox by utilizing a for each loop statement

I have a 2D array containing longitudes and latitudes that I need to map using MapBox. The example I found only demonstrates plotting two points, so I attempted to use a for-each loop to iterate through my 2D array and plot all the points. However, I enco ...

Transform JSON object to a class/interface object using Typescript

I am currently working on converting an API response into a TypeScript class or interface. The API is returning a list of objects with various properties, but I only require a few specific properties from the response object. Example of API Response: ...

Lazy loading a React grid gallery as you scroll

Currently, I am utilizing React grid gallery to showcase images from this repository: https://github.com/benhowell/react-grid-gallery. However, I am encountering an issue with lazy loading when the user scrolls on the page. <Gallery images={this ...

Is your sticky sidebar getting in the way of your footer?

I've developed a custom sticky sidebar for displaying ads, but I'm facing an issue. When I scroll to the bottom of the page, it overlaps with the footer. Can someone please take a look at this? - var stickySidebar = $('.sticky'); if ...

The Vue devServer proxy doesn't seem to be resolving the CORS error issue for me

Currently on @vue/cli 3.x and I've set up my vue.config.js with the following: devServer: { proxy: { "/api": { ws: true, changeOrigin: true, target: "http://localhost:8080" } } } Despite th ...

What is the quickest way to find and add together the two smallest numbers from a given array of numbers using JavaScript?

const getSumOfTwoSmallestNumbers = (numbers) => { const sortedNumbers = numbers.sort((a, b) => a - b); return sortedNumbers[0] + sortedNumbers[1]; } I encountered this challenge on Code Wars. My function to find the sum of the two smallest num ...

transform python string into json with proper structure

I am working on a code snippet that establishes parent-child relationships within a list of lists. Levels=[['L1','L1','L2'], ['L1','L1','L3'], ['L1','L2'], ...

AngularJS Date Formatting

I am facing an issue with binding a simple date object created in JavaScript to an input control using AngularJS. It seems like the problem might be related to the format of the date. Can you please help me understand why this happens? This problem specifi ...

Discovering the Week by week commencement and conclusion dates utilizing angularjs

Previously, I was utilizing the angularjs-DatePicker from npm. With this plugin, I could easily select a date from the calendar. However, now I require two fields - FromDate and ToDate - to display the week StartDate and EndDate whenever a date within that ...

The issue with Angular's mat-icon not displaying SVGs masked is currently being investigated

I have a collection of .svgs that I exported from Sketch (refer to the sample below). These icons are registered in the MatIconRegistry and displayed using the mat-icon component. However, I've observed issues with icons that utilize masks in Sketch ...

I am trying to figure out how I can include "all types of JSON data" in my typed request model when using a REST API. Can anyone

In my current project, I am utilizing .NET Framework and ASP.NET Core to develop a RESTful web API. This particular web api includes a method for receiving a request model to store information, as well as another method for later retrieving that data. Th ...

Changing $scope within an AngularJS service

I am currently working on an application that utilizes the Gmaps API for geolocalization. One of the challenges I faced was adding new markers based on user events. To address this, I created a service that listens to map events and adds markers when click ...

Managing the display of numerous ngFor components

If you're interested in learning more about the features I will include, here's a brief overview. I plan to have a project section with cards displayed for each project, all populated from a JSON file. When users click on a card on the website, a ...

How can we know when a JavaScript ES6 arrow function comes to a close?

One thing I've been pondering is how an arrow function terminates when it lacks brackets. I encountered this issue when working with ReactJS, where I had a function followed by the start of a class declaration. Here's the snippet: const Search = ...

Transform JSON data into a datatable by utilizing the rjsonio package

After successfully fetching properly formatted JSON data through a standard API, I encountered an issue. The API returns an array of JSON objects each time data is fetched, structured like this: [ {}, {}, {} ] Using a JSON editor, I confirmed that the ...

Steps to prevent inputting an entire JSON object into a sole field in AWS Athena

Currently, I am trying to import JSON data from S3 into an Athena table. The structure of my JSON data is as follows; [{"a":"a_value", "b":"b_value", "my_data":{"c":"c_value", "d&q ...

Removing an element from a dynamic dictionary in Python removes it from the entire dictionary, rather than just a specific key

Currently, I am developing a dynamic dictionary where I encountered an issue. I need to remove an element from a specific array within the dictionary without affecting the rest of the data. The problem arises when attempting to remove an item only from a p ...

Dealing with dynamic CORS settings in Apache and PHP

Dealing with CORS has been quite a challenge for me. My javascript is sending AJAX Put/Fetch requests to an Apache/PHP script. In this particular scenario, the javascript is being executed on CodePen while the Apache/PHP script is hosted on a local serve ...