Search an array for matching attribute

前端 未结 8 823
北荒
北荒 2020-12-04 14:16

I have an array, I need to return a restaurant\'s name, but I only know the value of its \"food\" attribute (not it\'s index number).

For example, how could I return

8条回答
  •  旧时难觅i
    2020-12-04 14:42

    let restaurant = restaurants.find(element => element.restaurant.food == "chicken");
    

    The find() method returns the value of the first element in the provided array that satisfies the provided testing function.

    in: https://developer.mozilla.org/pt-PT/docs/Web/JavaScript/Reference/Global_Objects/Array/find

提交回复
热议问题