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

Incorporating and retrieving JSON data saved within vuex storage

I am working with a vuex store to add JSON data in the following format: [ { "id":1, "firstname": "toto", "lastname": "titi" }, { "id":2, "firstn ...

Tips for integrating Material UI with Gatsby site generator

I'm diving deep into React by setting up the Gatsby kit with this site generator and Material UI framework. However, I encountered an error that says: Cannot read property 'prepareStyles' of undefined at RaisedButton.render I initiall ...

Having difficulty in displaying database values in HTML modal using PHP

On my PHP page, I have a setup that displays data based on the fetched id from the URL. Everything is working smoothly, but when I click a button to show a modal with additional information, the modal appears blank. Here is the code snippet I am using: ...

What causes the DOM's appendChild() to trigger on('load', ...) while jQuery's append() does not fire?

I have two snippets of code that I am working with: $(document).ready(function() { document.head.appendChild( $('<script />').attr('src', 'source.js').on('load', function() { ... ...

Techniques, modules and functions in Javascript

How should I properly document this code snippet: // Define a collection of colors with methods colors = { // Define method for color red "red" : function() { // Do something... } // Define object for color black "black" : { // Add ...

Can Javascript (PWA) be used to detect fake GPS or mock GPS in applications?

Looking for a solution to prevent users from using Fake Location tools in my PWA application that gathers absence location data. Is there a method or package in JavaScript to detect the presence of Fake GPS installed on the device? ...

Loading slides in bxSlider using ajax

Is there a method to dynamically insert a slide into bxSlider via ajax without affecting its smooth transition? I am looking to initially display the contents of only one slide upon page load, and then have subsequent slides loaded when the next or previo ...

Vue.js V-for not rendering any content

I am currently working on populating an array with objects retrieved from an API. The objects are being fetched successfully, but I am facing issues when trying to display them using v-for in the array. Below are my data variables: data() { return { ...

Practical strategy for developing and launching a TypeScript/Node.js application

I'm currently developing a node.js app using Typescript, which requires compilation to JS before running. As someone with a background in java/jvm, I'm hesitant about the deployment process where code is pushed to git, built/compiled on the serve ...

Troubleshooting Vue's reactivity problem with updating array element properties

I'm experiencing a reactivity problem in the example below. I can't figure out what I'm doing wrong. Is my vue data set up correctly or is there something else I need to do? Here's how my object model looks: export default { data ...

Guide to receiving dynamic argument variables in jQuery using $.get

I am currently working on developing an Ajax function call. In this function, the argument q will be dynamically defined, and the $.get method will return either true or false based on the data received from the Ajax call. <a href="any.php" class ...

Learning how to replace the alert function with Bootbox

I am currently working on a form that posts to a MySQL database. I want to replace the alert function triggered by the Malsup jQuery Form Plugin with the one created by the Bootbox plugin. Even though both plugins are functional, I struggle to integrate th ...

Learn how to create a smooth and consistent horizontal image slider using JavaScript to manipulate transition transforms

Does anyone know how to create a seamless sliding effect for a series of images in Vuejs? Let's say we have images 1, 2, 3, 4, and 5 When the first slide occurs, the order of images should be: 2, 3, 4, 5, 1 For the second slide: 3, 4, 5, 1, 2 And ...

Background image of HTML Iframe element does not display - Inline style block

https://i.stack.imgur.com/JW26M.png After setting the iframe's innerHTML using this line of code: iframedoc.body.innerHTML = tinyMCE.activeEditor.getContent(); The styles for the elements within the iframe are contained in an inline style block. Ho ...

What causes the variance in behavior between the Angular-formly directive and type?

I am facing an issue with two input fields that are both generated using the same template. I have set the required attribute to true for both of them by using the following code snippet: ... templateOptions: { ... required: true } One input fiel ...

Possible solution for resolving the issue: How to address the error message stating that "'v-model' is not suitable for use on a prop as local prop bindings are not writable"?

I am attempting to implement a dropdown sorting feature and encountered the following error: VueCompilerError: v-model cannot be used on a prop, because local prop bindings are not writable. Use a v-bind binding combined with a v-on listener that emits u ...

Maintain the chosen month in every dropdown toggle div in angular 4

While displaying data using toggle options, I am facing an issue where if I click on a different month, all other greyed out headers are displaying the previously selected values. I am trying to figure out a way to keep the value under Selected month as i ...

AngularJS allows for the creation of cascading dropdown selects, where the options in the second select

I'm struggling to access the version data stored in the server model, but it's not cooperating. My suspicion is that when the page loads, the initial data from the first select isn't available yet because it hasn't technically been sel ...

Tips for implementing picker options to limit the starting date in the Element UI date-picker

I'm currently developing a web application using Element UI, with a specific focus on the el-date-picker component. Here is the code I have written for defining a period using two calendars. However, I am facing difficulty in applying picker-options ...

Unable to access ConnectWise API due to receiving an Invalid Token response

I have been attempting to access ConnectWise records through the ConnectWise API as shown in the images linked below: https://i.stack.imgur.com/ur9iW.png and https://i.stack.imgur.com/FPzBC.png However, each time I make a request, I receive an Invalid ...