Skip to content
Advertisement

Trying to return a filter() value from an array of objects based on conditionals

I’m trying to return only a part of my object as an array based on conditionals but I’m kinda stumped.

I have an array of objects and I want to return an array of names for each key : value they fit in.

I do get only the unique pairings but I’m returning the food key:value as well and all of it is still inside an object not a new array. Some insight would be greatly appreciated. Newer to coding.

JavaScript

Advertisement

Answer

You’re really close here. What you need to incorporate is .map() to map your list of objects to a list of names. Your filter part works, partly by accident, so I’ve fixed it to be more correct (return names.food === 'oranges'), and then once you have your list of objects that match ‘oranges’ for their food, you map that filtered list into a list of names by doing .map(names => names.name)

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement