edit2: Tim Supinie在注释中提到,这可能会导致解释器超过调用堆栈大小。这可能取决于js引擎,但我也得到了“最大调用堆栈大小超过”在Chrome上至少。测试用例:[].concat.apply([], Array(300000).fill().map(_=>[1,2,3]))。(我使用目前接受的答案也得到了同样的错误,所以一个人正在预测这样的用例或为其他人构建一个库,无论您选择哪种解决方案,特殊的测试都是必要的。)
let arrOfArr = [[1,2,3,4],[5,6,7,8]];
let newArr = [];
for (let arr of arrOfArr) {
newArr.push(...arr);
}
console.log(newArr);
//Output: [1,2,3,4,5,6,7,8];
或者如果你喜欢函数式编程
let arrOfArr = [[1,2,3,4],[5,6,7,8]];
let newArr = arrOfArr.reduce((result,current)=>{
result.push(...current);
return result;
});
console.log(newArr);
//Output: [1,2,3,4,5,6,7,8];
或者使用ES5语法更好,没有展开操作符
var arrOfArr = [[1,2,3,4],[5,6,7,8]];
var newArr = arrOfArr.reduce((result,current)=>{
return result.concat(current);
});
console.log(newArr);
//Output: [1,2,3,4,5,6,7,8];
let Array = [
['vue','babel','npm','gulp','mysql','less','laravel'],
['jquery','react','js','css','wordpress','html','bootstrap'],
['vue','babel','npm','gulp','mysql','less','laravel'],
['angular','cms','js','css','graphql','nodejs','php'],
['severless','headless','js','css','design','photoshop','php'],
]
const Boom = new Set([].concat(...Array));
// This is not necessary
let dStr = '';
Boom.forEach(e=>{
dStr += e + ' ';
})
document.write(dStr);