在javascript中将多个二维数组合并为一个二维数组
Merge multiple two-dimensional arrays into one two-dimesional array in javascript
如何将这些数组合并为一个合并数组?
数据来自 table 行和列。
一个数组包含一整列的数据。
我想将这些列合并为一个包含 3 列的数组。
//array from col 1
let firstArray = [
[100], //row 1, col 1
[200], //row 2, col 1
[300] //row 3, col 1
]
//array from col 2
let secondArray = let firstArray = [
['a'], //row 1, col 2
['b'], //row 2, col 2
['c'] //row 3, col 2
]
//array from col 2
let secondArray = let firstArray = [
['run'], //row 1, col 3
['hide'], //row 2, col 3
['seek'] //row 3, col 3
]
预期的合并数组输出:
let mergedArray = [
[100, 'a', 'run'], //row 1, col 123
[200, 'b', 'hide'], //row 2, col 123
[300, 'b', 'seek'] //row 3, col 123
]
const firstArray = [
[100], //row 1, col 1
[200], //row 2, col 1
[300] //row 3, col 1
];
const secondArray = [
['a'], //row 1, col 2
['b'], //row 2, col 2
['c'] //row 3, col 2
];
const thirdArray = [
['run'], //row 1, col 3
['hide'], //row 2, col 3
['seek'] //row 3, col 3
];
const mergedArray = firstArray.map((item1, i) => {
const item2 = secondArray[i];
const item3 = thirdArray[i];
return [item1[0], item2[0], item3[0]];
});
console.log(mergedArray);
如何将这些数组合并为一个合并数组? 数据来自 table 行和列。
一个数组包含一整列的数据。
我想将这些列合并为一个包含 3 列的数组。
//array from col 1
let firstArray = [
[100], //row 1, col 1
[200], //row 2, col 1
[300] //row 3, col 1
]
//array from col 2
let secondArray = let firstArray = [
['a'], //row 1, col 2
['b'], //row 2, col 2
['c'] //row 3, col 2
]
//array from col 2
let secondArray = let firstArray = [
['run'], //row 1, col 3
['hide'], //row 2, col 3
['seek'] //row 3, col 3
]
预期的合并数组输出:
let mergedArray = [
[100, 'a', 'run'], //row 1, col 123
[200, 'b', 'hide'], //row 2, col 123
[300, 'b', 'seek'] //row 3, col 123
]
const firstArray = [
[100], //row 1, col 1
[200], //row 2, col 1
[300] //row 3, col 1
];
const secondArray = [
['a'], //row 1, col 2
['b'], //row 2, col 2
['c'] //row 3, col 2
];
const thirdArray = [
['run'], //row 1, col 3
['hide'], //row 2, col 3
['seek'] //row 3, col 3
];
const mergedArray = firstArray.map((item1, i) => {
const item2 = secondArray[i];
const item3 = thirdArray[i];
return [item1[0], item2[0], item3[0]];
});
console.log(mergedArray);