Search an array for matching attribute

前端 未结 8 804
北荒
北荒 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 14:55

    you can also use the Array.find feature of es6. the doc is here

    return restaurants.find(item => {
       return item.restaurant.food == 'chicken'
    })
    

提交回复
热议问题