js 数组 按列循环二维数组

期待效果:

核心代码:

java 复制代码
//js
function handle(array) {
    var result = [];
    for (let i = 0; i < array[0].length; i++) {
        var item = []; 
        for (let j = 0; j < array.length; j++) {
            item.push(array[j][i])
        } 
    	result.push(item);
    } 
	return result;
}

运行代码:

javascript 复制代码
let arr = [
  ['a1', 'b1', 'c1'],
  ['a2', 'b2', 'c2'],
  ['a3', 'b3', 'c3'],
];
function handle(array) {
  var result = [];
  for (let i = 0; i < array[0].length; i++) {
    var item = [];
    for (let j = 0; j < array.length; j++) {
      item.push(array[j][i]);
    }
    result.push(item);
  }
  return result;
}
console.log("数组>>>", arr);
console.log("按列循环二维数组>>>", handle(arr));

控制台运行效果:

相关推荐
inksci1 天前
用飞帆做了个小网页
前端·javascript·css·html
zhensherlock1 天前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js
多加点辣也没关系1 天前
JavaScript|第40章:JavaScript 工程化实践
开发语言·javascript·ecmascript
彩虹暴龙兽1 天前
JavaScript 预编译机制与执行上下文构建分析
javascript
tryCbest1 天前
Vue3——第 22 篇:Pinia——新一代状态管理(上)
前端·javascript·vue.js
bai2731 天前
"👨‍👩‍👧‍👦" 的 length 是 11:我把自己写的字数统计脚本拖出来审了一遍
前端·javascript
bai2731 天前
同一个 1.005,toFixed 给 1.00、Math.round 给 1、Intl 给 1.01:999 个三分位小数的实测
javascript·后端
爱编程的171 天前
一篇讲透 JS 对象:数据类型、原型链与包装类
javascript
Highcharts.js1 天前
Highcharts漏斗图表使用配置指南
java·开发语言·javascript·信息可视化·highcharts·漏斗图
武昌库里写JAVA2 天前
SpringBoot+Vue 项目介绍
java·javascript·git·sql·ai