查找根据角JS属性值列表中的一个对象 [英] Find an object in a list based on attribute value in Angular JS
本文介绍了查找根据角JS属性值列表中的一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种简单的方法来查找基于属性值的列表中的对象,不用循环就行了?
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/
这篇关于查找根据角JS属性值列表中的一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文