Make a simple loop using the splice method to remove the array:
var events = [{markets:[{outcomes:[{test:x},...]},...]},...];
for (var i=0; i<events.length; i++) {
var mrks = events[i].markets;
for (var j=0; j<mrks.length; j++) {
var otcs = mrks[j].outcomes;
for (var k=0; k<otcs.length; k++) {
if (! ("test" in otcs[k]))
otcs.splice(k--, 1);
}
if (otcs.length == 0)
mrks.splice(j--, 1);
}
if (mrks.length == 0)
events.splice(i--, 1);
}
This code will remove all results that do not have a property test, all empty markets, and all empty events from the array events.
The Underscore version might look like this:
events = _.filter(events, function(evt) {
evt.markets = _.filter(evt.markets, function(mkt) {
mkt.outcomes = _.filter(mkt.outcomes, function(otc) {
return "test" in otc;
});
return mkt.outcomes.length > 0;
});
return evt.markets.length > 0;
});
Bergi source
share