Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
Thank you. Only a curiosity: Which browsers does not support array.map() function? –  Calypo Gunn Jan 21 '13 at 17:21
1  
@CalypoGunn IE < 9. You can check it here: kangax.github.com/es5-compat-table. –  VisioN Jan 21 '13 at 17:31
    
Thank you very much VisioN. –  Calypo Gunn Jan 21 '13 at 19:07

The following should do the trick:

var myVar2 = [];
for (var i = 0; i < myVar1; i++) {
  myVar2.push([myVar1[i].myId, myVar1[i].myName]);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.