在Javascript文件中使用AngularJS过滤数组 [英] Filter array with AngularJS in Javascript file

查看:20
本文介绍了在Javascript文件中使用AngularJS过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组.如果有人预订了一张桌子,则数组中的reserve 设置为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;
};

如果您需要支持较旧的浏览器,则可以使用实现此行为的向后兼容功能 此处.

If you need to support older browsers there is a backward compatible function implementing this behaviour available here.

这篇关于在Javascript文件中使用AngularJS过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆