如何使用Underscore.js过滤器与对象? [英] How to use Underscore.js filter with an object?

查看:51
本文介绍了如何使用Underscore.js过滤器与对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的对象:

I have an object like so:

> Object
  > Rett@site.com: Array[100]
  > pel4@gmail.com: Array[4]
    > 0
       id : 132
       selected : true
    > 1
       id : 51
       selected : false

等..

如何使用下划线 _。filter( ) 只返回选中的项目=== true?

How can I use the underscore _.filter() to return back only the items where selected === true?

我从来没有必要下去使用 _。过滤器()的图层。类似

I've never had the need to go down to layers with _.filter(). Something like

var stuff = _.filter(me.collections, function(item) {
    return item[0].selected === true;
});

谢谢

推荐答案

如果要从选择 的任何电子邮件地址中提取所有数组元素 true ,你可以像这样迭代:

If you want to pull all array elements from any e-mail address where selected is true, you can iterate like so:

var selected = [];

for (email in emailLists) {
    selected.concat(_.filter(emailLists[email], function (item) {
        return item.selected === true;
    }));
}

如果您只想将数组拉到所有元素已选择,您可能会执行以下操作:

If you only want to pull the arrays where all elements are selected, you might instead do something like this:

var stuff = _.filter(me.collections, function(item) {
    return _.all(item, function (jtem) { 
        jtem.selected === true;
    });
});

这篇关于如何使用Underscore.js过滤器与对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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