Filtering through a multidimensional array using underscore.js

后端 未结 4 1635
-上瘾入骨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:15

    No need for Underscore, you could do this with native JS.

    var events = [{markets:[{outcomes:[{test:x},...]},...]},...];
    return events.filter(function(event) {
        return event.markets.some(function(market) {
            return market.outcomes.some(function(outcome) {
                return "test" in outcome;
            });
        });
    });
    

    Yet of course you could also use the corresponding underscore methods (filter/select and any/some).

提交回复
热议问题