A step-by-step guide on utilizing the v-for index loop to showcase a nested JSON array

My JSON Array has the following structure:

    items: [
     {
      ID: 11,
      UserID: "test.com",
      UserRole: "user",
      timeStamp: "2021-03-23T15:54:02.125578",
      dialogs: [
        {
          "Bot": "Why is data missing?",
          "test.com": "not available",
          "Bot": "please enter ID",
          "test.com": "1234"
        }
      ]
    }
  ]

I need to display elements inside dialogs as a list. I am using v-for, but dialogs are being displayed as an array with commas. How can I show this with an index? Here is the v-for loop I am using:

    <ul v-for="item in items" :key="item">
        <li v-for="(dialog, index) in dialogs" :key="index">{{ key }}: {{ dialog }}</li>
    </ul>

Answer №1

 <ul v-for="element in elements" :key="element">
      <li v-for="(conversation, number) in element.conversations" :key="number">{{number}}: {{conversation}}</li>
  </ul>

Answer №2

You will need to make 2 changes in order for the code to work correctly.

  • The keys within your dialogs are currently duplicates, so you should separate them into two distinct objects.
  • Additionally, you should iterate through the HTML using items.dialogs.

Below is the full revised code:

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!',
    items: [
     {
      ID: 11,
      UserID: "test.com",
      UserRole: "user",
      timeStamp: "2021-03-23T15:54:02.125578",
      dialogs: [
        {
          "Bot": "Why is data missing?",
          "test.com": "not available"
        },
          
        {"Bot": "please enter ID",
         "test.com": "1234"
        }
      ]
    }
   ]
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
 <ul v-for="(item, index) in items" :key="item">
     <li v-for = "(data, index) in item.dialogs"> 
 {{data}}  {{index}}</li>
    </ul>
</div>

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

Create an input element using JavaScript/jQuery

Looking for some help with Javascript on a simple task. When a specific option is chosen, I want to add an input field to a div element. <select name="amount" > <option value="50">50$</option> <option value="100">100$</o ...

Converting JSON into a List of strings

It seems like a simple task, but as a newcomer, I am struggling to quickly find the answer: I need to retrieve a list of file names (List<string>) in a .Net 2.0 desktop application using WebClient to make a request to a WebAPI REST service. Here is ...

Load select box with options upon document load

After the document loads, I want to populate a select box with values from my database using ajax and jQuery. Can someone help me identify what's wrong with my code? <select class="form-control sec" id="sec" name="sec"> <option value="s ...

Using async/await with Axios to send data in Vue.js results in different data being sent compared to using Postman

I am encountering an issue while trying to create data using Vue.js. The backend seems unable to read the data properly and just sends "undefined" to the database. However, when I try to create data using Postman, the backend is able to read the data witho ...

`Troubleshooting Firebase Cloud Functions and Cloud Firestore integration`

I previously used the following Firebase Database code in a project: const getDeviceUser = admin.database().ref(`/users/${notification.to}/`).once('value'); Now, I am attempting to convert it for Firestore. My goal is to retrieve my users' ...

Managing promises with mongoose - Best practices

I am new to using mongoose and I am trying to figure out how to save and handle promises in Node.js using a mongoose schema. In the example below, I am attempting to save data to a collection and handle any errors that may occur. model.js var mongoose = ...

Issues with the node.js and discord.js API due to superagent integration

Hey there, I've been working with an API that provides information based on ID inputs. Everything was running smoothly until I encountered an issue with invalid IDs. Instead of displaying a message like Invalid ID, my bot crashes when a wrong ID is en ...

Disable automatic playback of HTML video

There is an HTML video with an image that loads initially and then disappears to play the video. I would like the image to always be visible until I click on it. Once clicked, the video should start playing. You can view the code on JSFiddle: http://jsf ...

I seem to have mistakenly bound my conditional class to everything that was iterated in my object. Can you help me identify what I did wrong?

I recently received an object from a Last.FM API call containing the last 100 songs I listened to. If there is a song currently playing, there is a nowplaying flag on the first item in the object. I'm attempting to bind a class to the markup if this ...

Creating a new variable for every loop iteration in a foreach loop

I am attempting to create a for each loop that assigns a unique variable for each iteration of the array. Currently, when my loop runs, the variable $new only holds the final value, which is 17. Is there a way to make sure a different variable is assigned ...

Digital Repeater and Angle Measurer

Seeking Guidance: What is the recommended approach for locating the Virtual Repeaters in Protractor? A Closer Look: The Angular Material design incorporates a Virtual Repeater to enhance rendering performance by dynamically reusing visible rows in the v ...

Creating a global variable for both development and production APIs in Vue 3 is a key aspect in ensuring consistency

Creating a global variable that works for both production and development environments has been challenging. Despite efforts, the variable value remains undefined. I have set up .env and .env.prod files in the project's main directory. VUE_APP_ROOT_ ...

Unable to perform a post request with devise token authentication

I am facing an issue with making a post request to my controller endpoint. Interestingly, I was able to successfully create a user and retrieve all users using devise_token_auth. Following their documentation, I can perform CRUD operations on a user. Howe ...

Styling on Material UI Button disappears upon refreshing the page

I've been using the useStyles function to style my login page, and everything looks great except for one issue. After refreshing the page, all styles remain intact except for the button element, which loses its styling. Login.js: import { useEffect, ...

My React application came to an unexpected halt with an error message stating: "TypeError: Cannot read properties of undefined (reading 'prototype')"

Everything was running smoothly with my app until I encountered this error without making any significant changes: TypeError: Cannot read properties of undefined (reading 'prototype') (anonymous function) .../client/node_modules/express/lib/resp ...

Exploring the differences between conditional serialization using JAXB and Jackson: An insight into External and Internal Views

As I develop a RESTful API, I encounter a scenario where I must present two different perspectives of my data - one for internal use and another for external exposure. Furthermore, my API should accommodate both XML and JSON formats. In the case of JSON r ...

Issue with JQuery Validation: Some checkbox values are not being successfully posted

Currently experiencing issues with validating checkboxes. Utilizing the jQuery plugin validation found here: The scenario is that there are three checkboxes and at least one of them must be checked. The original form code for these checkboxes is as follow ...

What is the best way to eliminate the initial key from a JSON object

How can I remove the first index in a JSON object? JSON: {"data":[{"label":"Data","data":1},{"label":"Website","data":1}]} I need: [{"label":"Data","data":1},{"label":"Website","data":1}] When I try to delete .data, it outputs object{} JavaScript c ...

What are the steps to program a bot to respond to different types of media messages (such as pngs, mp4

I have been attempting to elicit a reaction from him based on the media message, but my attempts have been fruitless so far. It seems that the only time it reacts is when there is no text within the message... which complicates things. Can anyone provide s ...

Invalid JSON data in PHP

JSON Data { "data":{ "count":1, "orders":[ { "voucher_platform":0, "voucher":0.00, "order_number":270269205514864, "voucher_seller":0, "created_at":"2019-12-15 18:03:4 ...