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

控制台运行效果:

相关推荐
码艺-Alimjan22 分钟前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
可乐鸡翅yeah_1 小时前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
yivifu2 小时前
中文古籍电子书注释集成
前端·javascript·python·beautifulsoup·epub
weixin_448290253 小时前
书庐开发实战教学
javascript·css·html
默_笙3 小时前
🛴 从散件到整机:DeepAgents 与 Agent 身上预留的那些"插槽"(前置介绍)
前端·javascript
一木 之林3 小时前
DeepSeek Agent 开发(一)
开发语言·前端·javascript
樱花落木兰5 小时前
分布式登录实战:Session 会话共享改造,Redis 存储用户登录状态
java·javascript·数据库·redis·分布式·缓存
liangshanbo12156 小时前
面试题:线上 JavaScript 报错如何快速定位到源码?
开发语言·javascript·ecmascript
专业程序开发源7 小时前
django新闻推荐系统70655-计算机课程设计、毕业设计
java·javascript·spring boot·后端·python·django·课程设计
acd120098 小时前
前端随笔:数据明明变了,Vue 页面就是不更新
前端·javascript·vue.js