在JavaScript中连接N个对象数组的最有效的方法是什么?

数组是可变的,结果可以存储在一个输入数组中。


当前回答

[].concat.apply([], [array1, array2, ...])

效率证明:http://jsperf.com/multi-array-concat/7

Tim Supinie mentions in the comments that this may cause the interpreter to exceed the call stack size. This is perhaps dependent on the js engine, but I've also gotten "Maximum call stack size exceeded" on Chrome at least. Test case: [].concat.apply([], Array(300000).fill().map(_=>[1,2,3])). (I've also gotten the same error using the currently accepted answer, so one is anticipating such use cases or building a library for others, special testing may be necessary no matter which solution you choose.)

其他回答

最快的10倍是迭代数组,就像它们是一个数组一样,而不实际连接它们(如果可以的话)。

我很惊讶concat比push稍微快一点,除非测试不公平。

const arr1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']; const arr2 = ['j', 'k', 'l', 'i', 'm', 'n', 'o', 'p', 'q', 'r', 's']; const arr3 = ['t', 'u', 'v', 'w']; const arr4 = ['x', 'y', 'z']; let start; // Not joining but iterating over all arrays - fastest // at about 0.06ms start = performance.now() const joined = [arr1, arr2, arr3, arr4]; for (let j = 0; j < 1000; j++) { let i = 0; while (joined.length) { // console.log(joined[0][i]); if (i < joined[0].length - 1) i++; else { joined.shift() i = 0; } } } console.log(performance.now() - start); // Concating (0.51ms). start = performance.now() for (let j = 0; j < 1000; j++) { const a = [].concat(arr1, arr2, arr3, arr4); } console.log(performance.now() - start); // Pushing on to an array (mutating). Slowest (0.77ms) start = performance.now() const joined2 = [arr1, arr2, arr3, arr4]; for (let j = 0; j < 1000; j++) { const arr = []; for (let i = 0; i < joined2.length; i++) { Array.prototype.push.apply(arr, joined2[i]) } } console.log(performance.now() - start);

如果你抽象它,你可以让不加入的迭代更干净,它仍然是原来的两倍:

const arr1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']; const arr2 = ['j', 'k', 'l', 'i', 'm', 'n', 'o', 'p', 'q', 'r', 's']; const arr3 = ['t', 'u', 'v', 'w']; const arr4 = ['x', 'y', 'z']; function iterateArrays(arrays, onEach) { let i = 0; while (joined.length) { onEach(joined[0][i]); if (i < joined[0].length - 1) i++; else { joined.shift(); i = 0; } } } // About 0.23ms. let start = performance.now() const joined = [arr1, arr2, arr3, arr4]; for (let j = 0; j < 1000; j++) { iterateArrays(joined, item => { //console.log(item); }); } console.log(performance.now() - start);

新回答

对于多个数组和ES6的数组,使用

arr.flat();

例如:

const arr = [[1, 2, 3], [4, 5, 6], [7, 8 ,9]];
const newArr = arr.flat();
// output: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

这将适用于节点> 11和现代浏览器。


旧的答案

(把它留在这里,以防旧版本的节点需要它):

对于多个数组和ES6的数组,使用

Array.prototype.concat(...arr);

例如:

const arr = [[1, 2, 3], [4, 5, 6], [7, 8 ,9]];
const newArr = Array.prototype.concat(...arr);
// output: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

这样解决。

let arr = [[1, 2], [3, 4], [5, 6]];
 console.log([].concat(...arr));

用ES6缩短。

new Set([].concat(...Array));

这可以连接并唯一多个数组;

let Array = [ [“vue”,“巴别塔”,“npm”,“杯”,“mysql”,“少”,“laravel”), (“jquery”、“反应”,js, css, wordpress,“html”,“引导”), [“vue”,“巴别塔”,“npm”,“杯”,“mysql”,“少”,“laravel”), (‘角’,‘cms’,‘js’,‘css’,‘graphql’,‘nodejs ', ' php '], (“severless”、“无头”、“js”,“css”,“设计”,“ps”,“php”), ] const Boom = new Set([].concat(…数组)); //不需要 let dStr = "; Boom.forEach (e = > { dStr += e + ' '; }) document . write(下游); < div class = "结果" > < / div >

concat()方法用于连接两个或多个数组。它不会更改现有数组,只返回已连接数组的副本。

array1 = array1.concat(array2, array3, array4, ..., arrayN);