Filtering through a multidimensional array using underscore.js

后端 未结 4 1677
-上瘾入骨i
-上瘾入骨i 2021-01-12 05:58

I have an array of event objects called events. Each event has markets, an array containing market objects.

4条回答
  •  一个人的身影
    2021-01-12 06:34

    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 ;
            });
        });
    });
    

提交回复
热议问题