我有一组名为 events
的 event
对象。每个 event
都有 markets
,这是一个包含 market
对象的数组。这里有另一个名为 outcomes
的数组,包含 outcome
对象。
我想使用 Underscore.js 或其他一些方法来查找所有具有市场的事件,这些事件的结果具有名为 test
的属性。
我想这可以使用一系列过滤器来实现,但我运气不佳!
最佳答案
我认为您可以使用 Underscore.js 来做到这一点 filter
和 some
(又名“任何”)方法:
// 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 ;
});
});
});
关于javascript - 使用 underscore.js 过滤多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10821657/