首页 > 解决方案 > 将两个数组组合成对

问题描述

我有两个想要组合的数组,但在搜索中我只能找到Array.concat.

let a = [1,2,3,4,5]
let b = [6,7,8,9,10]

我如何结合这些来创建以下内容?

let combine = [
  [1,6],
  [2,7],
  [3,8],
  [4,9],
  [5,10]
]

标签: javascriptarrays

解决方案


如果两个数组的长度相同,则可以执行以下操作:

let a = [1,2,3,4,5]
let b = [6,7,8,9,10]
let combine = a.map((e, i) => [e, b[i]]);
console.log(combine);


推荐阅读