1

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.

13
  • Can you post a sample array? Commented Jun 7, 2018 at 4:06
  • @Eddie Please see update, tried just using fixed array values, and works. but when I used the database from database, it's not working anymore. Maybe I think it's because of my array result format? Commented Jun 7, 2018 at 5:08
  • maybe try to "console.log(Object.keys(findGroupId))" and check if the the key is group_id without any spaces. Commented Jun 7, 2018 at 5:10
  • @DF No displayed value. Commented Jun 7, 2018 at 5:12
  • But logging findGroupId still gives you the output of your first screenshot? Commented Jun 7, 2018 at 5:14

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

There you go. That's it. Thanks :)
Happy to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.