1

我有这个对象数组

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

我试图得到这种格式作为最终结果:[["thisA","thisB","thisC"], ["thatA","thisB","thatC"]]

我知道我们可以使用带有特定键(A、B、C)的 map() 函数。

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

但是我需要一个通用的函数来传递它而不使用密钥,因为数组的内容会不同,密钥也会不同。有什么好的解决办法吗?

4

2 回答 2

6

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

const result = arr.map(Object.values)

console.log(result);

于 2018-12-21T08:23:26.747 回答
0

我赞成 punksta 提供优雅的解决方案,但这就是我在自动模式下的做法(不考虑如何使其优雅):

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)

于 2018-12-21T08:30:36.993 回答