2

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

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Only a curiosity: Which browsers does not support array.map() function?
@CalypoGunn IE < 9. You can check it here: kangax.github.com/es5-compat-table.
1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.