user6632933
user6632933

Reputation:

Returns undefined when finding an item in an array in VUE

I have the code below that finds the item details in array which works well.

var findGroupId = medias.find(medias => medias.group_name === this.groupName)

My medias is an array variable. Then the result of console.log(findGroupId) is below:

enter image description here

But when I do console.log(findGroupId.group_id) the result is undefined. But when I try changing the demo here under Using ES2015 arrow function and do demo on JavaScript Demo: Array.find(), it works well. Am I missing something?

Sample array output (fetched from database): enter image description here PS: trying not to use for loop to save some memory and time.

Upvotes: 1

Views: 986

Answers (1)

Eddie
Eddie

Reputation: 26844

this inside the arrow function might be referring to the document and not the object. One option is to store the this.groupName on a local variable and use it on your condition.

var groupName = this.groupName;
var findGroupId = medias.find(media => media.group_name === groupName);

Upvotes: 1

Related Questions