Search an array for matching attribute

前端 未结 8 824
北荒
北荒 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:33

    you can use ES5 some. Its pretty first by using callback

    function findRestaurent(foodType) {
        var restaurant;
        restaurants.some(function (r) {
            if (r.food === id) {
                restaurant = r;
                return true;
            }
       });
      return restaurant;
    }
    

提交回复
热议问题