Synay
Synay

Reputation: 133

Array Object to Array List

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

Answers (4)

Sven.hig
Sven.hig

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

sonEtLumiere
sonEtLumiere

Reputation: 4562

With map method and Object.values

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

Upvotes: 2

lei li
lei li

Reputation: 1342

let b = a.map((ite)=>[ite.id,ite.name])

Upvotes: 2

Unmitigated
Unmitigated

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

Related Questions