0

I'm trying to do a for loop of a set of objects but since they all aren't named the same, I'm stuck.

<div id="components-demo">
    <div>Travel Information</div>
    <ul>
        <li 
          v-for="(todo, index) in todos"
          v-bind:id="index"
          v-bind:title="todo"
            >{{todo}}</li>
    </ul>
</div>


var newData = @Html.Raw(Json.Encode(Model));
// Object returns like { Passenger: "Tom Jones", Airline: "United Airways", Destination: "Atlanta, GA", etc. }    

var vm = new Vue({
    el: '#components-demo',
    data: {
        todos: [
            { newData }
        ]
    }
})

In the developer tools in Vue, it lists out the object fine like:

todos: Array [1]
   0: Object
     Passenger: "Tom Jones"
     Airline: "United Airways"
     Destination: "Atlanta, GA"
     etc.

At the end I'm looking to list out li's containing these items but can't seem to loop through unless I specify exactly each one.

2
  • "they all aren't named the same" You mean they have different keys? I suspect you need a second loop to iterate over the object keys as well. Commented Jan 7, 2019 at 7:54
  • Well like since they aren't all called Passenger, I can't do newData.Passenger perse Commented Jan 7, 2019 at 7:58

2 Answers 2

1

According to the object you say is constructed.

<div id="components-demo">
    <div>Travel Information</div>
    <ul>
        <li v-for="(item, index) in todos" :key="index">{{ item.Passenger }}</li>
    </ul>
</div>

To list out dynamic objects using nested loop:

<div id="components-demo">
    <div>Travel Information</div>
    <ul>
        <li v-for="(item, index) in todos" :key="index">
          <ul>
             <li v-for="(value, key) in item" :key="key">{{ key }} : {{ value }}</li>
          </ul>
        </li>
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

This worked thanks. I see I needed to add in item, index instead of using the object itself
1

You are assigning the object to an array, you can just loop through the object directly https://jsfiddle.net/cckLd9te/4656/

data: {
    todos: newData 
  },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.