【发布时间】:2021-05-12 13:19:47
【问题描述】:
我有两个数组,如下所示。我正在尝试使用 array_1 中的 field 键和 array_2 中的值来创建一个新的对象数组。
const result = []
array_1 = [{ name: "Color" , field: "color"}, {name: "Shape", field: "shape" }, { name: "Whatever", field: "whatever" }]
array_2 = [["green", "rectangular", "whatever1"], ["yellow", "circle", "whatever2"]]
结果应该是:
console.log(result)
// [{color:"green", shape:"rectangular", whatever: "whatever1"},
// { color:"yellow", shape: "circle", whatever:"whatever2"}]
我在最后的审判中这样做了:
const rowObj = {}
const result = array.map((subarray) => subarray.map((cell, index) => {
console.log(cell,index)
rowObj[columns[index].field] = cell
return rowObj
}))
基本上,我正在覆盖同一个对象。
谢谢,
【问题讨论】:
-
你试过自己做吗?
-
是的,我做到了。我的结果是 [ [{}] , [{}] ]。
-
请在您的问题中包含该代码。
-
感谢您的帮助
标签: javascript arrays ecmascript-6