简体   繁体   English

如何将这两个数组合并为一个

[英]How to merge these two array into one

Could anyone please advice how to merge two arrays (same length) into one ?任何人都可以请教如何将两个数组(相同长度)合并为一个? Thank you!谢谢! The spread operator didnt work in that way..传播运算符没有以这种方式工作..

let Array1 = [[A,B],[C,D]];
let Array2 = [1,2];

let Array3 = [...Array1, Array2];



Use a forEach loop使用forEach循环

 var arr=[['A','B'],['C','D']]; var arr2 = [1,2]; arr.map((i,j)=>i.push(arr2[j])) console.log(arr)

Using .map() and .concat() you can combine the arrays.使用.map().concat()可以组合数组。

Try the following:请尝试以下操作:

 let Array1 = [['A','B'],['C','D']]; let Array2 = [1,2]; const result = Array1.map((e, i) => e.concat(Array2[i])); console.log(result);

I hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM