2

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 ?

2
  • 1
    In your code the call to sampleArray[names] would return undefined... it's an array you cannot access it this way. Commented Dec 3, 2013 at 12:56
  • @Arno2501 this i put for reference, intentions are to show that i want names there... Commented Dec 4, 2013 at 4:14

1 Answer 1

7

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.net/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

Sign up to request clarification or add additional context in comments.

8 Comments

okay got your point, but this is good for if first element is an array of two elements. What if it holds more than two values, I mean we don't know how many values it holds. I mean any generic way of doing this. for example: if sampleArray = [["name1","age1","empcode"....],["name2","age2","empcode"....]] kind of pattern. Where we are not sure with the no. of elements.
Well if you are to work with arrays the only way is to have an index there is no other way. But you could try to work with objects that have named properties instead. Do you get me ?
Btw it will still work if you had any number of sub elements to your array as long as the index that you seek do not move.
Not exaactly able to get, "work with objects that have named properties:" you means instead of passing array to async.mapSeries i can pass object ? or you mean passing array which contains object. sampleArray = [{name:"abc",age:23}, {name:"def", age: 24}]. Which way it will be right ?
Can you please show a concrete example of your data (at least the structure) as it is really. And explain the end result that you seek. Because it's hard to guess.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.