滤波器阵列在Javascript文件AngularJS [英] Filter array with AngularJS in Javascript file
问题描述
我有一个数组。如果有人预订餐桌,阵列中的储备设置为true。
I've got an array. If someone reserve a table, reserve in the array is set to true.
$rootScope.tafels = [
{id: 0, text:'table 2a, 4 persons.', reserve:false},
{id: 1, text:'table 3b, 8 persons.', reserve:false}
];
和我有一个函数返回数组的长度:
And I've got an function for returning the length of the array:
$rootScope.getTotaalTafels = function()
{ return $rootScope.tafels.length; };
现在困难的部分,我解决不了,也许你可以:
Now the difficult part that I can not solve, maybe you can:
我要回未保留的,我上面的功能显示的总表。我该如何申请一个过滤器呢?
I want to return the total tables that are not reserved, with my function showed above. How do I apply a filter to it?
推荐答案
JavaScript 1.6中实现了过滤功能,可正是这一点:
Javascript 1.6 implements the filter function which allows exactly this:
$rootScope.getTotaalTafels = function(){
return $rootScope.tafels.filter(function(value,index){
return !value.reserve;
}).length;
};
如果您需要支持旧的浏览器有实现这种行为可<一个向后兼容功能href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter?redirectlocale=en-US&redirectslug=JavaScript/Reference/Global_Objects/Array/filter\"相对=nofollow>这里。
If you need to support older browsers there is a backward compatible function implementing this behaviour available here.
这篇关于滤波器阵列在Javascript文件AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!