通过使用underscore.js多维数组过滤 [英] Filtering through a multidimensional array using underscore.js

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

问题描述

事件称为对象事件的数组。每个事件市场,包含数组市场的对象。这里面有一个叫另一个数组结果,载结果的对象。

I have an array of event objects called events. Each event has markets, an array containing market objects. Inside here there is another array called outcomes, containing outcome objects.

我想用Underscore.js或其他方法来查找所有具有具有其中有一个命名属性成果市场事件的测试

I want to use Underscore.js or some other method to find all of the events which have markets which have outcomes which have a property named test.

我想这将使用一系列的过滤器来实现,但是我并没有太多的运气!

I imagine this would be achieved using a series of filters but I didn't have much luck!

推荐答案

我觉得你可以使用Underscore.js 做到这一点过滤器 部分 (又名任)方法:

I think you can do this using the Underscore.js filter and some (aka "any") methods:

// filter where condition is true
_.filter(events, function(evt) {

    // return true where condition is true for any market
    return _.any(evt.markets, function(mkt) {

        // return true where any outcome has a "test" property defined
        return _.any(mkt.outcomes, function(outc) {
            return outc.test !== undefined ;
        });
    });
});

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

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