我有两个2D数组和什么合并行和行。
ARR1=[[a,b,c],[d,e,f],[g,h,i]]
ARR2=[[1,2,3],[4,5,6],[7,8,9]]
我想要这样的输出
ARR2=[[a,b,c,1,2,3],[d,e,f,4,5,6],[g,h,i,7,8,9]
我如何用map方法做到这一点?
null
const arr1 = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
];
const arr2 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
const result = arr1.map((arr1Item, i) => [...arr1Item, ...arr2[i]]);
console.log(result);
您可以将concat
与map
:
null
var arr1 = [['a','b','c'],['d','e','f'],['g','h','i']];
var arr2 = [[1,2,3],[4,5,6],[7,8,9]];
var result = arr1.map((k,i)=>k.concat(arr2[i]));
console.log(result);
你可以像这样使用地图,地图和推送。。。
null
const foo = [
['a','b','c'],
['d','e','f'],
['g','h','i']
]
const bar = [
[1,2,3],
[4,5,6],
[7,8,9]
]
foo.map((el, i) => {
el.map((e) => {
bar[i].push(e)
})
})
console.log(bar)