我有两个数组
var arr1 = [0,1,2,3,4]
var arr2 = [23,32,11,35,15]
我怎么得到这个?
var result = [[0,23],[1,32],[2,11],[3,35],[4,15]]
答案 0 :(得分:1)
简单来说,请执行以下操作:
var arr1 = [0,1,2,3,4];
var arr2 = [23,32,11,35,15];
var result = [];
if (arr1.length === arr2.length)
result = arr1.map(function (cur, idx) {
return [cur, arr2[idx]];
});
console.log(result);
使用ES 6箭头功能的更短版本:
var arr1 = [0,1,2,3,4];
var arr2 = [23,32,11,35,15];
var result = [];
if (arr1.length === arr2.length)
result = arr1.map((cur, idx) => [cur, arr2[idx]]);
console.log(result);
答案 1 :(得分:1)
假定两个输入数组的长度相同:
var result = arr1.map((item, idx) => [item, arr2[idx]])