Guide to iterating through an array within an object in Vue.js

Can someone help me with looping through data fetched from an API using a GET request in Vue.js?

This is the sample response obtained:

"data": {
     "Orders": [
               {
            "OrderID": 1,
            "Ordered_Products": {
                            "items": [
                                         {
                                    "id": 2,
                                    "title": "Hildegard Prohaska",
                                    "quantity": 5,
                                    },
                                    {
                                    "id": 3,
                                    "title": "Odell Zieme",
                                    "quantity": 3,
                                    }
                                ]
                               },
            "Pay_method": 1,
            //stuff
       },
    {
            "OrderID": 1,
            "Ordered_Products": {
                            "items": [
                                         {
                                    "id": 2,
                                    "title": "Hildegard Prohaska",
                                    "quantity": 2,
                                    },
                                    {
                                    "id": 3,
                                    "title": "Odell Zieme",
                                    "quantity": 1,
                                    }
                                ]
                               },
            "Pay_method": 2,
            //stuff
       }
   ]
}

Below is the fetch operation I'm using:

methods: {
    fetchOrders () {
      fetch('https://myapidomine.com')
        .then(res => res.json())
        .then(res => {
          this.orders = res.data.Orders
        })
    }
  }

And here's how I'm implementing it:

<v-card flat v-for="order in orders" :key="order.OrderID">
        <v-layout row wrap">
          <v-flex xs12 md4>
            <div class="caption grey--text">PayMethod</div>
            <div>{{ order.Pay_method }}</div>
          </v-flex> 

I can access keys inside Orders, like {{order.Pay_method}}, without any issues. However, I'm struggling to loop through items within Ordered_Products.items and retrieve details such as title.

If I try:

<v-list-item-title xs12 md3>{{order.Ordered_Products.items[0].title}}

I can only get data for the first product. Can anyone guide me on how to properly loop through this data?

Thank you in advance! (I'm still new to Vue)

Answer №1

I can't view your complete code, but a solution similar to the following should work:

<v-list-item-title xs12 md3 :key="index" v-for="(item, index) in order.Ordered_Products.items">
   {{item.title}}
</v-list-item-title>

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

What is the best way to distribute a function within a div container?

I'm currently working on a function that manages the show/hide functionality and position of tooltips: tooltip = (e) => { // show/hide and position of tooltip // retrieve element data } In addition, I have div elements whe ...

Adjusting the dimensions of the box while the clock is ticking

I am looking to automatically resize a cube whenever a new value is entered in the input text box. I have found a similar solution that updates the cube size only when a button is clicked. http://jsfiddle.net/EtSf3/3/ document.getElementById('btn&ap ...

What is the best way to combine a JSON response object with the variable J inside a for loop?

Received a JSON response as follows: { "name" : "chanchal", "login3" : "1534165718", "login7" : "1534168971", "login6" : "1534168506", "login5" : "1534166215", "login9" : "1534170027", "login2" : "1534148039", "lastname" : "khandelwal", ...

Style the code presented within a div tag

I am in the process of creating a JavaScript-powered user interface that can generate code based on user interactions. While I have successfully implemented the code generation functionality and saved the generated code as a string, I am facing difficultie ...

Are there any notifications triggered when a draggable element is taken out of a droppable zone?

Within a single droppable area, there is a collection of individual Field Names that can be dragged, and a table featuring X headers, each of which can be dropped into. Initially, these headers are empty. Is there a way to detect when an item is taken out ...

Data sent through AJAX messaging is not being acknowledged

I recently made an AJAX request and set it up like this: $.ajax({ data : { id : 25 }, dataType : 'json', contentType : 'application/json; charset=utf-8', type : 'POST', // the rest of the ...

Ways to prevent the loading of images during webpage loading

I have encountered an issue with my eCommerce site developed using Laravel 7. Whenever I click on the category page, all product images are being loaded, causing high bandwidth usage. The category "Appereal" contains over 100 products, and although I imple ...

What's preventing me from using the left click function on my published blog post?

This is my first time creating a blogger template and everything seems to be working correctly. However, I have encountered one problem. For some reason, I am unable to left click on my blog post. I have not installed any right/left click disabler and I&a ...

Having trouble with the post method being not allowed in your SPA Vue on Laravel application? Have you tried

This question has been raised multiple times, with varying solutions that have not proven to be effective. Despite my attempts to set the csrf-token in different ways, I still can't get it to work. My goal is to create a Single Page Application (SPA) ...

Notification continuously appears when clicking outside of Chrome

I have implemented the use of onblur on a text box to display an alert. However, I encountered an issue where if I click outside my Chrome browser after entering text in the text box and then click on the Chrome browser button in the task bar, the alert ap ...

What is the best way to make multiple HTML tables sortable?

I recently implemented an open-source JavaScript table sorter in my project. You can find more information about it here: However, I encountered an issue where only the most recently added table on my page is sortable. When users press a button, new table ...

Is it possible to execute an ajax function at regular intervals until a specific condition is fulfilled?

My application consists of two JSP pages. One is responsible for the UI, while the other processes the response. UICounter.jsp: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Page</titl ...

The iPad Air 2 experiencing issues with rendering on three.js

Recently, I've transitioned to using an iPad Air 2 for work and decided to explore equirectangular panoramas. While these panoramas work perfectly on my desktop, I was excited about utilizing the device orientation features on my iPad/iPhone. However ...

Simply output the integer value

Recently, I've been working on a function that I'm trying to condense into a one-liner code for a challenge on Codewars. You can find the problem here. Here's the code snippet that I currently have: export class G964 { public static dig ...

Map does not provide zero padding for strings, whereas forEach does

Currently working on developing crypto tools, I encountered an issue while attempting to utilize the map function to reduce characters into a string. Strangely enough, one function works perfectly fine, while the other fails to 0 pad the string. What could ...

I'm having trouble getting Socket.io to function properly with my Node/Express application

Using openshift with express, I have been experiencing difficulties configuring socket.io. No matter what adjustments I make, it seems to disrupt my application. What could be the issue? Interestingly, when I disable the sections related to socket.io, the ...

What is the best way to incorporate a new attribute into an array of JSON objects in React by leveraging function components and referencing another array?

Still learning the ropes of JavaScript and React. Currently facing a bit of a roadblock with the basic react/JavaScript syntax. Here's what I'm trying to accomplish: import axios from 'axios'; import React, { useState, useEffect, useMe ...

HTML Menu with Ajax functionality designed to work seamlessly without relying on JavaScript

I'm working on making my navigation system compatible with both clients who have JavaScript disabled and those who can use Ajax. Currently, I have dynamic links generated inside the navigation like "index.php?page=/cat/page.php". <li id="sidebarit ...

Issue with vuejs: npm run dev command not functioning

I'm facing an issue with running VueJS webpack on a server using the command: npm run dev. Instead of it running smoothly, I am getting a long list of errors. You can view the screenshot of the errors by clicking here. ...

Discover the most effective method for identifying duplicate items within an array

I'm currently working with angular4 and facing a challenge of displaying a list containing only unique values. Whenever I access an API, it returns an array from which I have to filter out repeated data. The API will be accessed periodically, and the ...