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
  • 354
  • 1
  • 5
  • 22

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
  • 34,372
  • 9
  • 61
  • 83
punksta
  • 2,738
  • 3
  • 23
  • 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
  • 19,522
  • 3
  • 54
  • 89
  • 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