1

I have this array of objects

[{
    "A": "thisA",
    "B": "thisB",
    "C": "thisC"
}, {
    "A": "thatA",
    "B": "thatB",
    "C": "thatC"
}]

I'm trying to get this format as an end result: [["thisA","thisB","thisC"], ["thatA","thisB","thatC"]]

I know we can use map() function with the specific key(A, B, C).

newarray = array.map(d => [d['A'], d['B'], d['C']])

But I need a common function to transfer it without using the key, because the content of array will be different, the key will be different. Is there any good solution?

Ian
  • 344
  • 1
  • 5
  • 21

2 Answers2

6

const arr = [{
  "A": "thisA",
  "B": "thisB",
  "C": "thisC"
}, {
  "A": "thatA",
  "B": "thatB",
  "C": "thatC"
}]

const result = arr.map(Object.values)

console.log(result);
adiga
  • 31,610
  • 8
  • 53
  • 74
punksta
  • 2,588
  • 2
  • 22
  • 41
0

I've upvoted punksta for elegant solution, but this is how I would do that in automatic mode (without thinking how to make it elegant):

const src = [{
    "A": "thisA",
    "B": "thisB",
    "C": "thisC"
}, {
    "A": "thatA",
    "B": "thatB",
    "C": "thatC"
}]

const result = src.map(o => Object.keys(o).map(k => o[k]))

console.log(result)
Nurbol Alpysbayev
  • 16,001
  • 2
  • 41
  • 68
  • I wouldn't say the other answer is _to make it elegant_, I'll say _efficient_, and even more readable than this. – Asons Dec 21 '18 at 08:39
  • @LGSon You are not wrong. I added my answer just because I wanted to. Because of reasons. And for comparison also, maybe. – Nurbol Alpysbayev Dec 21 '18 at 08:40