简体   繁体   English

用javascript文件中的AngularJS过滤数组

[英]Filter array with AngularJS in Javascript file

I've got an array. 我有一个数组。 If someone reserve a table, reserve in the array is set to true. 如果有人保留表,则将数组中的reserve设置为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 implements the filter function which allows exactly this: Javascript 1.6实现了筛选器功能,该功能完全允许:

$rootScope.getTotaalTafels = function(){
    return $rootScope.tafels.filter(function(value,index){
        return !value.reserve;
    }).length;
};

If you need to support older browsers there is a backward compatible function implementing this behaviour available here . 如果您需要支持较旧的浏览器,可以在此处使用向后兼容的功能来实现此行为。

在最后一个AngularJS版本中使用$filter ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM