0
for (var i = 0; i < dataSets.length; i++) {
        var result = _.filter(data, function(item){
          return _.contains(item, dataSets[i]);
        });
        var collection = []
        for(x in result)collection.push(result[x].value);
    }

When I do a console.log(collection) inside the method, I can see 3 arrays, which is correct.

[431, 552, 318, 332, 185]
[17230, 17658, 15624, 16696, 9276]
[5323, 6359, 8216, 9655, 5513]

However outside of the method I can only get the last value.

[5323, 6359, 8216, 9655, 5513]

Is there a way I can return all the values outside of the method?

dave
  • 355
  • 5
  • 11
  • define an external "collectionArray" (outside of the for scope) and push the collection into it after. Or use the map function of dataSets, assuming it's an array and that you use a relatively modern browser) – Raanan W Feb 23 '15 at 23:35
  • your collection[] array gets overwritten on each iteration of for...loop so move it out of for...loop and it should work.. – Sudhir Bastakoti Feb 23 '15 at 23:37
  • Don't use [`for in` loops](http://stackoverflow.com/q/500504/1048572)! – Bergi Feb 23 '15 at 23:52

2 Answers2

0

You can add each collection to an array:

var collections = [];
for (var i = 0; i < dataSets.length; i++) {
    var result = _.filter(data, function(item){
      return _.contains(item, dataSets[i]);
    });
    collections[i] = [];
    for(x in result) collections[i].push(result[x].value);
}
// Now you have all values into "collections"
// If you are within a method you can also "return collections;" here
floribon
  • 19,175
  • 5
  • 54
  • 66
0

If you're in an ES6 state of mind:

dataSets . map(set => data .
    filter(item => item.contains(set)) .
    map   (item => item.value)
)