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
Add a ment  | 

1 Answer 1

Reset to default 8

I 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