查找根据角JS属性值列表中的一个对象属性、根据、对象、列表中

2023-09-13 04:08:20 作者:自找没趣

有没有一种简单的方法来查找基于属性值的列表中的对象,不用循环就行了?

Is there a simple way to find an object in a list based on an attribute value, without looping on the list?

例如给出类似下面的列表:

For example given a list like the following :

var lst = [{name : "foo", value : "fooValue"}, {name: "bar", value: "barValue"}];

是有某种找到的方法,这样, lst.find(名,富)将返回其中有一个名称的对象,其值为foo的属性?

is there some kind of "find" method, such that lst.find("name", "foo") would return the object which has a "name" attribute with the value "foo"?

推荐答案

您可以使用$过滤服务:

You can use the $filter service:

angular.module('app', [])

function ParentCtrl($scope, $filter){
    var lst = [{name : "foo", value : "fooValue"}, {name: "bar", value: "barValue"}, { name: 'foo', value: 'something else'}];
    var newTemp = $filter("filter")(lst, {name:'foo'});
    console.log(newTemp);
}

的jsfiddle : http://jsfiddle.net/W2Z86/