我有两个数组 一个是:
[ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ] ]
另一个是:
[ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ]
如何连接两者并得到像
这样的输出[ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ],
[ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ]
答案 0 :(得分:1)
var arr1 = [ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ] ];
var arr2 = [ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ];
arr1.push(arr2);
答案 1 :(得分:0)
您是否尝试过使用点差运营商?请注意,IE不支持它。
const x =
[ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ] ];
const y = [ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ];
const z = [...x, y];
console.log(z);
答案 2 :(得分:0)
您可以将附加数组包装在额外的括号中,以便与Array#concat
一起使用。
var a = [[2, 'c'], [2, 'e'], [3, 'a'], [3, 'b']],
b = [3, [1, 'g'], [2, [1, 'd'], [1, 'f']]];
console.log(a.concat([b]));

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)
concat
会将两个数组合并为一个数组而不更改原始数组
const x =
[ [ 2, 'c' ],
[ 2, 'e' ],
[ 3, 'a' ],
[ 3, 'b' ] ]
const y = [ 3, [ 1, 'g' ], [ 2, [ 1, 'd' ], [ 1, 'f' ] ] ];
const z = x.concat([y])
const x = [
[2, 'c'],
[2, 'e'],
[3, 'a'],
[3, 'b']
]
const y = [3, [1, 'g'],
[2, [1, 'd'],
[1, 'f']
]
];
const z = x.concat([y])
console.log(z)