Javascript 使用 es6 spread 连接多个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43455911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using es6 spread to concat multiple arrays
提问by danday74
We all know you can do:
我们都知道你可以做到:
let arr1 = [1,2,3];
let arr2 = [3,4,5];
let arr3 = [...arr1, ...arr2]; // [1,2,3,3,4,5]
But how do you make this dynamic to concat N arrays?
但是你如何使这个动态连接 N 个数组呢?
回答by Brian McCutchon
One option is to use reduce:
一种选择是使用reduce:
let arrs = [[1, 2], [3, 4], [5, 6]];
arrs.reduce((a, b) => [...a, ...b], []);
Of course, this is a slow solution (quadratic time). Alternatively, if you can use Lodash, _.flattendoes exactly what you want, and does it more efficiently (linear time).
当然,这是一个缓慢的解决方案(二次时间)。或者,如果您可以使用 Lodash,_.flatten完全按照您的意愿执行操作,并且效率更高(线性时间)。
EDIT
编辑
Or, adapted from Xotic750's comment below,
或者,改编自 Xotic750 下面的评论,
[].concat(...arrs);
Which should be efficient (linear time).
这应该是有效的(线性时间)。
回答by acontell
Another option could be:
另一种选择可能是:
const nArrays = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9],
[10, 11]
];
const flattened = [].concat(...nArrays);
console.log(flattened)
回答by AJEET SINGH
let fruits = ["apples", "bananas", "pears"];
let vegetables = ["corn", "potatoes", "carrots"];
let produce = [...fruits, ...vegetables];
console.log(produce);
回答by Pankaj Shrivastava
Following solution works for me (spread operator in ES6):
以下解决方案适用于我(ES6 中的扩展运算符):
let array = ['my','solution','works'];
let newArray = [];
let newArray2 = [];
newArray.push(...array); //adding to same array
newArray2.push([...array]); //adding as child/leaf/sub-array
console.log(newArray);
console.log(newArray2);
回答by Pedro Castilho
You can't do that with spread syntax alone, as spread syntax requires you to know how many arrays you are concatenating in advance. However, you could write the following function:
您不能单独使用扩展语法来做到这一点,因为扩展语法要求您提前知道要连接的数组数量。但是,您可以编写以下函数:
function concatN(...arguments) {
let accumulator = [];
for(let arg = 0; arg < arguments.length; arg = arg + 1) {
accumulator = [...accumulator, ...arguments[arg]];
}
return accumulator;
}
It probably won't be very efficient, though (repeated use of spread syntax is O(n2)). Using Array.prototype.concatwould be better. You can just do:
不过,它可能不会非常有效(重复使用扩展语法是 O(n2))。使用Array.prototype.concat会更好。你可以这样做:
[].concat(all, of, your, arrays);
回答by guest271314
You can use spread element within for..ofloop to concatenate array values to a single array
您可以在for..of循环中使用 spread 元素将数组值连接到单个数组
let arr1 = [1,2,3];
let arr2 = [3,4,5];
let arr3 = [];
for (let arr of [arr1, arr2 /* , arrN */]) arr3.push(...arr);
console.log(arr3);
回答by Thank you
You could use a recursive function and Array.prototype.concat
您可以使用递归函数和 Array.prototype.concat
const concatN = (x,...xs) =>
x === undefined ? [] : x.concat(concatN(...xs))
console.log(concatN([1,2,3], [4,5,6], [7,8,9]))
// [1,2,3,4,5,6,7,8,9]
You can do the same thing using reduceand Array.prototype.concat. This is similarto the accepted answer but doesn't senselessly use spread syntax where x.concat(y)is perfectly acceptable (and likely heaps faster) in this case
您可以使用reduceand做同样的事情Array.prototype.concat。这类似于已接受的答案,但x.concat(y)在这种情况下不会无谓地使用完全可以接受的传播语法(并且可能堆得更快)
const concatN = (...xs) =>
xs.reduce((x,y) => x.concat(y), [])
console.log(concatN([1,2,3], [4,5,6], [7,8,9]))
// [1,2,3,4,5,6,7,8,9]
回答by KARTHIKEYAN.A
According to es6
根据es6
function mergeTwo(arr1, arr2) {
let result = [...arr1, ...arr2];
return result.sort((a,b) => a-b);
}

