admin管理员组文章数量:1310050
I have an object:
var sampleArray = [["name1", "age1"], ["name2", "age2"],["name3", "age3"]];
async.mapSeries(sampleArray[names], sampleFunction(), function(err, result) {
console.log(result);
});//sample code //["name1","name2","name3"] executes here
async.mapSeries(sampleArray[ages], sampleFunction(), function(err, result) {
console.log(result);
});//sample code //["age1","age2","age3"] executes here
This is my sample code, here I want to achieve first time all the "name" properties got executed in samplefunction and on the second iteration all the "age" properties.
How to achieve that ?
I have an object:
var sampleArray = [["name1", "age1"], ["name2", "age2"],["name3", "age3"]];
async.mapSeries(sampleArray[names], sampleFunction(), function(err, result) {
console.log(result);
});//sample code //["name1","name2","name3"] executes here
async.mapSeries(sampleArray[ages], sampleFunction(), function(err, result) {
console.log(result);
});//sample code //["age1","age2","age3"] executes here
This is my sample code, here I want to achieve first time all the "name" properties got executed in samplefunction and on the second iteration all the "age" properties.
How to achieve that ?
Share Improve this question edited Dec 3, 2013 at 10:58 robieee asked Dec 3, 2013 at 10:46 robieeerobieee 3642 silver badges19 bronze badges 2-
1
In your code the call to
sampleArray[names]
would return undefined... it's an array you cannot access it this way. – Arno 2501 Commented Dec 3, 2013 at 12:56 - @Arno2501 this i put for reference, intentions are to show that i want names there... – robieee Commented Dec 4, 2013 at 4:14
1 Answer
Reset to default 8I think what you want is this :
var sampleArray = [["name1", "age1"], ["name2", "age2"],["name3", "age3"]];
async.mapSeries(sampleArray, function(data,callback){
return callback(null, data[0]);
}, function(err, results) {
console.log('results : ' + results); // results : name1,name2,name3
});
//sample code //["name1","name2","name3"] executes here
async.mapSeries(sampleArray, function(data,callback){
return callback(null, data[1]);
}, function(err, results) {
console.log('results : ' + results); // results : age1,age2,age3
});
//sample code //["age1","age2","age3"] executes here
http://jsfiddle/9bdWT/12/
Edit with object data :
var sampleData = [{'name':'name1', 'age':'age1'},{'name':'name2', 'age':'age2'},{'name':'name3', 'age':'age3'}];
async.mapSeries(sampleData, function(data,callback){
return callback(null, data['name']);
}, function(err, results) {
console.log('results : ' + results); // results : name1,name2,name3
});
This way you can access the name property without having to worry with indexes
本文标签: javascriptHow to use asyncmapseries with Arrays (Holding arrays)Stack Overflow
版权声明:本文标题:javascript - How to use async.mapseries with Arrays (Holding arrays) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741808035a2398625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论