我有一个数组。如果有人预订了一张 table ,则数组中的reserve将设置为true。
$rootScope.tafels = [
{id: 0, text:'table 2a, 4 persons.', reserve:false},
{id: 1, text:'table 3b, 8 persons.', reserve:false}
];
我有一个返回数组长度的函数:
$rootScope.getTotaalTafels = function()
{ return $rootScope.tafels.length; };
现在我无法解决的困难部分,也许你可以:
我想返回未保留的总表,我的函数如上所示。如何对其应用过滤器?
最佳答案
Javascript 1.6 实现了过滤器功能,它允许这样做:
$rootScope.getTotaalTafels = function(){
return $rootScope.tafels.filter(function(value,index){
return !value.reserve;
}).length;
};
如果您需要支持较旧的浏览器,可以使用向后兼容的功能来实现此行为 here .
关于javascript - 在 Javascript 文件中使用 AngularJS 过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16815320/