最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filtering through a multidimensional array using underscore.js - Stack Overflow

programmeradmin2浏览0评论

I have an array of event objects called events. Each event has markets, an array containing market objects. Inside here there is another array called outcomes, containing outcome objects.

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.

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

I have an array of event objects called events. Each event has markets, an array containing market objects. Inside here there is another array called outcomes, containing outcome objects.

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.

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

Share Improve this question edited Dec 24, 2015 at 21:13 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked May 30, 2012 at 17:55 user1082754user1082754 2
  • Is there any inverse reference? I mean from outcome to market and so on. Because in this case you can just filter all the outcome objects those match your search and go backwards until the event, then clean up the array for unique elements. – fguillen Commented May 30, 2012 at 18:00
  • Can you provide an example of this? – user1082754 Commented May 30, 2012 at 18:13
Add a comment  | 

4 Answers 4

Reset to default 14

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

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

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).

Try this:

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

DEMO

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;
});
发布评论

评论列表(0)

  1. 暂无评论