javascript - 在递增的同时从嵌套数组中提取元素
问题描述
我想从数组中的每个数组中提取每个元素。例如:
const arr = [[a, b, c], [d, e, f], [g, h, i], [j, k, l]];
我想遍历嵌套数组,提取第一个元素(a、d、g、j)。然后我想迭代嵌套数组,提取第二个元素(b,e,h,k),依此类推......
我正在努力弄清楚我将如何实现这一目标?
解决方案
您可以使用第一个元素的长度进行迭代
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; }
推荐阅读
- python - 将 pyautogui 与多个监视器一起使用
- python - 解析 json 文件以收集数据并存储在列表/数组中
- java - 如何在方法中使用 switch 语句,以便该方法可以返回一个 char 值,该值由其中的 switch 语句获取?
- sql - 在 2 列中拆分特殊字符并在 oracle 中合并为多行
- c# - Grpc.Core.RpcException StatusCode Unavailable Channel 处于状态 TRANSIENT_FAILURE
- python-3.x - 如何处理纬度和经度的 5 位或 6 位数字值?
- java - 如何让2个线程共享数据?(爪哇)
- pyspark - 使用 Python 在 Databricks 中使用 XGBoost
- css - 可滚动的 Ionic 4 选项卡
- c++ - 创建 alignas(64) 整数的向量