Suppose I have an array of nested objects:
this_menu = [{food_name: 'hamburger', food_attr: ['big', 'yummy', 'cheap']}, {food_name: 'sushi', food_attr: ['healthy', 'expensive', 'yummy']}, {food_name: 'tacos', food_attr: ['yummy', 'cheap', 'small']}]
If I want to find the the nested object that contains 'sushi', I could simply call this_array[1]
by index. But lets say, I want to find it strictly by the key - food_name's value. I want to do this without using a for loop if possible and in a single line if it can be done. I basically want to return the object: {food_name: 'sushi', food_attr: ['healthy', 'expensive', 'yummy']}
. I tried the following, but I think the syntax is wrong (throws a syntax error). Any suggestions on getting this to work:
tonights_dinner = this_menu.map((x) => if(x.food_name == `sushi') { return x});