8 javascript arrays object underscore.js
我有一个event名为的对象数组events.每个event都有markets一个包含market对象的数组.在这里有另一个叫做outcomes包含outcome对象的数组.
我想使用Underscore.js或其他一些方法来查找具有市场的所有事件,这些市场的结果具有一个名为的属性test.
我想这可以通过一系列过滤器实现,但我没有太多运气!
McG*_*gle 12
我认为你可以使用Underscore.js filter和some(又名"any")方法来做到这一点:
// 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 ;
});
});
});
Run Code Online (Sandbox Code Playgroud)