首页 > 解决方案 > 在递增的同时从嵌套数组中提取元素

问题描述

我想从数组中的每个数组中提取每个元素。例如:

const arr = [[a, b, c], [d, e, f], [g, h, i], [j, k, l]];

我想遍历嵌套数组,提取第一个元素(a、d、g、j)。然后我想迭代嵌套数组,提取第二个元素(b,e,h,k),依此类推......

我正在努力弄清楚我将如何实现这一目标?

标签: javascriptarrays

解决方案


您可以使用第一个元素的长度进行迭代

const arr = [
  ["a", "b", "c"],
  ["d", "e", "f"],
  ["g", "h", "i"],
  ["j", "k", "l"],
]

for (let i = 0; i < arr[0].length; i++) {
  const temp = arr.map((el) => el[i])
  console.log(temp)
}
.as-console-wrapper { max-height: 100% !important; }


推荐阅读