Answer the question
In order to leave comments, you need to log in
How to display the elements of a nested object?
Help with the output of the elements.
I display an object
<ul class="list-group" id="v-for-object">
<li v-for="value in object" class="list-group-item">{{ value }}</li>
</ul>
data: {
object1: {
firstName: 'Bruce Lee',
userName: 'Dragoon',
email: '[email protected]',
adress: {
country: 'China',
town: 'Gong-kong'
},
}
Answer the question
In order to leave comments, you need to log in
You can make a method that transforms an object so that there is no nesting:
methods: {
plainObj(obj) {
return Object.entries(obj).reduce((acc, [ k, v ]) => {
if (v instanceof Object) {
Object.assign(acc, this.plainObj(v));
} else {
acc[k] = v;
}
return acc;
}, {});
}
}
<li v-for="value in plainObj(object)" class="list-group-item">{{ value }}</li>
Vue.component('list-obj', {
template: `
<ul class="list-group">
<li v-for="value in obj" class="list-group-item">
<list-obj v-if="value instanceof Object" :obj="value"></list-obj>
<span v-else>{{ value }}</span>
</li>
</ul>`,
props: [ 'obj' ]
});
<list-obj :obj="object"></list-obj>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question