Reputation:
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:
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):
PS: trying not to use for loop to save some memory and time.
Upvotes: 1
Views: 986
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