Reputation: 133
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"]
]
Upvotes: 0
Views: 82
Reputation: 4519
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)
Upvotes: 1
Reputation: 4562
With map method and Object.values
let newArr = a.map(x => Object.values(x));
Upvotes: 2
Reputation: 89224
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);
Upvotes: 1