1

Couldn't find a better title, my issue is hard to describe in one sentence. I'm trying to loop through an imported object generated by some third party package, but its structure is problematic:

obj: {
  "name1": {
    "property1": "value1",
    "property2": "value2",
  },
  "name2": {
    "property1": "value3",
    "property2": "value4",
  },
  "name3": {
    "property1": "value5",
    "property2": "value6",
  }
}

I want to display not just property1 and property2 but also the name preceding these:

- name1
  value1
  value2

- name2
  value3
  value4

...

This is the code I have so far:

<ul>
  <li v-for="(item, i) in obj" :key="index">
    {{ item.property1 }}
    <br>
    {{ item.property2 }}
  </li>
</ul>

How can I display the name as well, given this object' odd structure?

JSFiddle

2 Answers 2

1

Just add key property in your v-for loop and render it :

 <li v-for="(item,key, i) in obj" :key="index">
     {{key}}
      <br>
     {{ item.property1 }}
      <br>
      {{ item.property2 }}
  </li>
Sign up to request clarification or add additional context in comments.

Comments

0

Loop the object key with Object.keys(), and display the object value from the key with another v-for

<script src="https://unpkg.com/vue"></script>

<div id="app">
  <ul>
    <li v-for="(item, i) in Object.keys(obj)" :key="index">
      {{ item }}
      <ul>
        <li v-for="(property, key) in obj[item]" :key="property">
          {{ key }} -> {{ property }}
        </li>
      </ul>
    </li>
  </ul>
</div>

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.