将每个数组的子元素组合成一个新的最终数组 - JavaScript

swo*_*888 3 javascript arrays ecmascript-6

我有一个这样的数据结构:

const arr1 = [["name1", "123", "prop2"],["name2", "43", "prop22"], ["name3", "22", "prop3"]];
const arr2 = [[156, 154, "position report"],[173, 124, "position report"],[136, 154, "position report"]];
Run Code Online (Sandbox Code Playgroud)

我希望最终的数组看起来像这样:

finalArr = [["name1", "123", "prop2",156, 154, "position report], ["name2", "43", "prop22",173, 124, "position report"],["name3", "22", "prop3", 136, 154, "position report"]]
Run Code Online (Sandbox Code Playgroud)

基本上我想将 arr1 到 arr2 的子数组元素合并到一个新的最终数组中。我尝试使用此代码,但它仅合并 2 个数组,而不合并子元素

let newArr = [];
arr1.forEach((item) => {
arr2.forEach((element) => {
  newArr.push({ ...item, element });
   });
});

console.log(newArr);
Run Code Online (Sandbox Code Playgroud)

有什么只使用 ES6 符号的想法吗?

mic*_*ckl 5

您可以使用array.map转换源数组,i作为索引来获取相应的元素,并使用扩展运算符连接两个数组:

const arr1 = [["name1", "123", "prop2"],["name2", "43", "prop22"], ["name3", "22", "prop3"]];
const arr2 = [[156, 154, "position report"],[173, 124, "position report"],[136, 154, "position report"]];

let result = arr1.map((item, i) => [...item, ...arr2[i]]);
console.log(result);
Run Code Online (Sandbox Code Playgroud)