Get an object from array which contains a specific value

前端 未结 2 913
南方客
南方客 2020-12-14 15:14

I am trying to search through an array of objects using Underscore.js, but I can\'t seem to target the one I want.

console.log(_.findWhere(response.data, { T         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-14 15:55

    Use _.find instead of findWhere:

    console.log(_.find(response.data, function(item) {
        return item.TaskCategory.TaskCategoryId == $routeParams.TaskCategory; 
    }));
    

    They are similar, but findWhere is designed for special cases where you want to match key-value pairs (not useful in your scenario as it involves nested objects). Find is more general-use, because it lets you provide a function as the predicate.

提交回复
热议问题