Heim > Fragen und Antworten > Hauptteil
比如这样:
var array1 = ["one","two","three'];
var array2 = ["1","2","3"];
怎么将这两个数组(假定两个数组长度相等)合并成var array = [["one","1"],["two","2"],["three","3"]]
?
有办法实现吗?新手求助!
怪我咯2017-04-11 13:12:38
Array.from还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。
Array.from(array1, (item, idx) => [item, array2[idx]])
巴扎黑2017-04-11 13:12:38
看到楼上的,突然想到类似的操作
var array1 = ["one","two","three"];
var array2 = ["1","2","3"];
var arr=[];
array1.forEach((currentValue,index)=>arr.push([currentValue,array2[index]]));
传统一点:
var array1 = ["one","two","three"];
var array2 = ["1","2","3"];
var arrayAll=[];
for(let i=0;i<array1.length;i++){
arrayAll.push([array1[i],array2[i]])
}