javascript 使用 underscore.js 过滤多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10821657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:09:26  来源:igfitidea点击:

Filtering through a multidimensional array using underscore.js

javascriptarraysobjectunderscore.js

提问by McGarnagle

I have an array of eventobjects called events. Each eventhas markets, an array containing marketobjects. Inside here there is another array called outcomes, containing outcomeobjects.

我有一个event名为events. 每个event都有markets,一个包含market对象的数组。里面还有另一个数组叫做outcomes,包含outcome对象。

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.

我想使用 Underscore.js 或其他一些方法来查找所有具有名为test.

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

我想这可以使用一系列过滤器来实现,但我运气不佳!

回答by McGarnagle

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

我认为你可以使用 Underscore.jsfiltersome(又名“任何”)方法来做到这一点:

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

回答by Bergi

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

不需要 Underscore,你可以用原生 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/selectand any/some).

当然,您也可以使用相应的下划线方法(filter/selectany/some)。

回答by webjay

var events = [
  {
    id: 'a',
    markets: [{
      outcomes: [{
        test: 'yo'
      }]
    }]
  },
  {
    id: 'b',
    markets: [{
      outcomes: [{
        untest: 'yo'
      }]
    }]
  },
  {
    id: 'c',
    markets: [{
      outcomes: [{
        notest: 'yo'
      }]
    }]
  },
  {
    id: 'd',
    markets: [{
      outcomes: [{
        test: 'yo'
      }]
    }]
  }
];

var matches = events.filter(function (event) {
  return event.markets.filter(function (market) {
    return market.outcomes.filter(function (outcome) {
      return outcome.hasOwnProperty('test');
    }).length;
  }).length;
});

matches.forEach(function (match) {
  document.writeln(match.id);
});

Here's how I would do it, without depending on a library:

这是我将如何做到这一点,而不依赖于图书馆:

var matches = events.filter(function (event) {
  return event.markets.filter(function (market) {
    return market.outcomes.filter(function (outcome) {
      return outcome.hasOwnProperty('test');
    }).length;
  }).length;
});

回答by qwertymk

Try this:

试试这个:

_.filter(events, function(me) { 
    return me.event && 
        me.event.market && me.event.market.outcome && 
        'test' in me.event.market.outcome
}); 

DEMO

演示