0

Which the best way to convert this array object:

a = [
 {"id" : 1, "name": "a"},
 {"id" : 2, "name": "b"},
 {"id" : 3, "name": "c"}
]

to:

b = [
 [1, "a"],
 [2, "b"], 
 [3, "c"]
]
Unmitigated
91.5k12 gold badges103 silver badges109 bronze badges
asked Jul 14, 2020 at 3:28
0

4 Answers 4

2
let b = a.map((ite)=>[ite.id,ite.name])
answered Jul 14, 2020 at 3:31
Sign up to request clarification or add additional context in comments.

Comments

2

With map method and Object.values

let newArr = a.map(x => Object.values(x));

answered Jul 14, 2020 at 3:33

Comments

1

map each element using Object.values.

const a = [
 {"id" : 1, "name": "a"},
 {"id" : 2, "name": "b"},
 {"id" : 3, "name": "c"},
]
const b = a.map(Object.values);
console.log(b);

answered Jul 14, 2020 at 3:29

Comments

1

Using map

a = [ {"id" : 1, "name": "a"}, {"id" : 2, "name": "b"}, {"id" : 3, "name": "c"} ]
r=a.map(o=>[o.id,o.name])
console.log(r)

answered Jul 14, 2020 at 3:38

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.