I've got the following object that's an object array (an array of objects), on a variable:
variable: myVar1
On fireBug I get the following lines when I call myVar1:
[Object { myId= "1", myName= "Name1" }, Object { myId= "2", myName= "Name2" }, Object { myId= "3", myName= "Name3" }]
I need this data to be in the followng format stored on a variable too:
myVar2 = [
[1, 'Name1'],
[2, 'Name2'],
[3, 'Name3']
]
I've tried so many things like the use of for loops and js functions but can't get it work. I supose it's so easy. Anyone could try to explain the procedure.
Thank you
asked Jan 21, 2013 at 17:04
Calypo Gunn
1771 gold badge2 silver badges9 bronze badges
2 Answers 2
Solution for browsers which support Array.map():
var result = arr.map(function(o) { return [+o.myId, o.myName]; });
For compatibility you may use the shim provided at MDN.
answered Jan 21, 2013 at 17:06
VisioN
146k35 gold badges287 silver badges291 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Calypo Gunn
Thank you. Only a curiosity: Which browsers does not support array.map() function?
VisioN
@CalypoGunn IE < 9. You can check it here: kangax.github.com/es5-compat-table.
The following should do the trick:
var myVar2 = [];
for (var i = 0; i < myVar1; i++) {
myVar2.push([myVar1[i].myId, myVar1[i].myName]);
}
answered Jan 21, 2013 at 17:07
lawnsea
6,5811 gold badge27 silver badges19 bronze badges
Comments
lang-js