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);
}
当我在方法中执行console.log(集合)时,我可以看到3个数组,这是正确的。
[431, 552, 318, 332, 185]
[17230, 17658, 15624, 16696, 9276]
[5323, 6359, 8216, 9655, 5513]
然而,除了方法之外,我只能得到最后一个值。
[5323, 6359, 8216, 9655, 5513]
有没有办法可以返回所有方法之外的值?
答案 0 :(得分:0)
您可以将每个集合添加到数组中:
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
答案 1 :(得分:0)
如果你处于ES6的心态:
dataSets . map(set => data .
filter(item => item.contains(set)) .
map (item => item.value)
)