How can I create a nested array in JavaScript? -
with code below:
var result = []; result.push({success : 1}); (var = 0; < rows.length; i++) { result.push({email : rows[i].email}); };
i create array looks this:
[ { "success": 1 }, { "email": "email1@email.com" }, { "email": "email2@email.com" }, { "email": "emailn@email.com" } ]
but want create array looks this:
[ { "success": 1 }, { "email": ["email1@email.com","email2@email.com","emailn@email.com"] } ]
i'm stuck on exact syntax doing this. how can put array inside array?
var result = [ { success : 1 }, { email : rows.map(function(row) { return row.email; }) } ];
some explanation: rows
array, , arrays in js have method .map()
can used process each item in array , return new array processed values.
for each item, function called value of item, , whichever value returned function added new array.
in case, function returns email
property each of items, end array of e-mail addresses, want.
edit: initial suggestion make result
object instead:
var result = { success : 1, email : rows.map(function(row) { return row.email; }) };
whether better depends on requirements structure of data.
Comments
Post a Comment