I am sending an article->tags
array of objects to my Vue component like so:
JavaScript
x
4
1
<article-form
2
:edit-data-tags="{{ $article->tags }}"
3
></article-form>
4
I get this array:
JavaScript
1
2
1
[ 0: { id:'1', name:'mytag' } ]
2
Now in my component, I want to get to the name field so I can store it and pass it onwards. How can i do this?
this post has the same problem, but when i try this solution:
JavaScript
1
6
1
created: function () {
2
for (let tag in this.editDataTags) {
3
console.log(tag.name)
4
}
5
}
6
I get an undefined.
Advertisement
Answer
for...in
loops are for objects, but you have an array. Try a for...of
loop:
JavaScript
1
4
1
for (let tag of this.editDataTags) {
2
console.log(tag.name)
3
}
4
or forEach
:
JavaScript
1
4
1
this.editDataTags.forEach(tag => {
2
console.log(tag.name);
3
});
4
or for
loop:
JavaScript
1
4
1
for (let i=0; i < this.editDataTags.length; i++) {
2
console.log(this.editDataTags[i].name)
3
}
4