0

I am sending an article->tags array of objects to my Vue component like so:

<article-form
    :edit-data-tags="{{ $article->tags }}"
></article-form>

I get this array:

[  0: { id:'1', name:'mytag' } ... ]

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:

created: function () {
   for (let tag in this.editDataTags) {
      console.log(tag.name)
   }
}

I get an undefined.

3
  • you said I get this array:, where do you get that? Commented Dec 3, 2020 at 19:08
  • @BoussadjraBrahim well $article->tags retrieves all tags related to said article from my tables. the Tags method is : -> return $this->belongsToMany(Tag::class)->withTimestamps(); <- In article model Commented Dec 3, 2020 at 19:28
  • Try to print that data inside a watcher property Commented Dec 3, 2020 at 19:44

1 Answer 1

0

for...in loops are for objects, but you have an array. Try a for...of loop:

for (let tag of this.editDataTags) {
   console.log(tag.name)
}

or forEach:

this.editDataTags.forEach(tag => {
   console.log(tag.name);
});

or for loop:

for (let i=0; i < this.editDataTags.length; i++) {
   console.log(this.editDataTags[i].name)
}
Sign up to request clarification or add additional context in comments.

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.