# JS 如何多个一维Array 竖向拼成新的二维Array

benniu
• 34

``````a = [1,2,3,4,5]
b = [11,22,33,44,55]
c = [111,222,333,444,555]``````

``````newArray = [
[ 'A','B','C'],  －〉可以先不考虑这个。后面可以再拼接。
[ 1, 11, 111],
[ 2, 22, 222],
[ 3, 33, 333],
[ 4, 44, 444],
[ 5, 55,555]
]``````

a,b,c 的长度是动态的

##### 3个回答
✓ 已被采纳

``````a = [1,2,3,4,5]
b = [11,22,33,44,55]
c = [111,222,333,444,555]

a.map((v,i)=>[a,b,c].map(v=>v[i]))``````

``````const a = [1, 2, 3, 4, 5]
const b = [11, 22, 33, 44, 55]
const c = [111, 222, 333, 444, 555]

const arr = a.reduce((accu, cur, index) => {
accu.push([a,b,c].map((v) => v[index]));
return accu
}, []);
console.log(arr);``````

1. 由于不定长，找到最长的那个元素（如果 `a`, `b`, `c` 数组长度相同，就不需要了，随便取一个就行）
2. 然后按这个长度循环，依次从 `a`, `b`, `c` 三个数组中取一个出来拼成新的数组。

``````const a = [1, 2, 3, 4, 5];
const b = [11, 22, 33, 44, 55];
const c = [111, 222, 333, 444, 555];

function transform(...args) {
// 找最大长度
const length = Math.max(...args.map(a => a.length));
// 转置
const matrix = Array.from({ length })
.map((_, i) => args.map(a => a[i]));

// 前面补列首
matrix.unshift(args.map((_, i) => String.fromCharCode(65 + i)));
return matrix;
}

console.log(transform(a, b, c));``````

``````function transform(...args) {
return Array
.from({ length: Math.max(...args.map(a => a.length)) })
.map((_, i) => args.map(a => a[i]));
}``````
##### 撰写回答
###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进