【问题标题】:Combine two arrays as pairs [duplicate]将两个数组组合成对[重复]
【发布时间】:2020-02-03 00:50:48
【问题描述】:

我有两个想要合并的数组,但在搜索中我只能找到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]
]

【问题讨论】:

标签: javascript arrays


【解决方案1】:

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

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2019-10-25
    • 2023-01-10
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多