Javascript:推送整个列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4842993/
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
Javascript: push an entire list?
提问by shoosh
Is there a built in way to append one list into another like so:
是否有一种内置的方法可以将一个列表附加到另一个列表中,如下所示:
var a = [1,2,3];
a.append([4,5]);
// now a is [1,2,3,4,5];
concat()
does something similar but returns the result. I want something that modifies the existing list like push()
concat()
做类似的事情,但返回结果。我想要一些修改现有列表的东西,比如push()
回答by outis
回答by Salvador Dali
var list = [1, 2];
People already showed you how to do this with:
人们已经向您展示了如何做到这一点:
- push:
list.push.apply(list, [3, 4]);
- concat:
list = list.concat([4, 5]);
- 推:
list.push.apply(list, [3, 4]);
- 连接:
list = list.concat([4, 5]);
But I want to tell about ES6 spread operator: list.push(...[3, 4]);
.
但我想谈谈ES6 扩展运算符: list.push(...[3, 4]);
。
Keep in mind that currently not many browsers support it (you can use transpilers).
请记住,目前支持它的浏览器并不多(您可以使用转译器)。
回答by Avindu Hewa
If you are using ES6 you can use the spread operator
如果您使用的是 ES6,则可以使用扩展运算符
eg :-
例如:-
listOne = [1,2,3]
listTwo = [4,5,6]
listTwo.push(...listOne)
回答by Darin Dimitrov
How about this:
这个怎么样:
var a = [1, 2, 3];
a = a.concat([4, 5]);
// a is now [1, 2, 3, 4, 5]
回答by Aloso
Because I just wrote a web application where a lot of arrays have to be concatenated and where performance is critical, I tested which method is fastest in this jsperf. The results are quite interesting.
因为我刚刚编写了一个 Web 应用程序,其中必须连接大量数组并且性能至关重要,所以我在这个 jsperf 中测试了哪种方法最快。结果非常有趣。
In my test, I add 10 elements to a list or array of 10,000 elements.
在我的测试中,我将 10 个元素添加到包含 10,000 个元素的列表或数组中。
Here are the test cases, from fastest to slowest. Results are measured in Chrome 62, but Firefox 47 performs similarly:
以下是测试用例,从最快到最慢。结果是在 Chrome 62 中测量的,但 Firefox 47 的表现类似:
LinkedList.prototype.concat
: 90,313,485 ops/seclist.concat(concatList); // This function has to change only 1-2 refences
Array.prototype.push
in a for loop: 3,794,962 ops/secfor (var i = 0, len = concatArr.length; i < len; i++) { array.push(concatArr[i]); } // Probably fastest in real life
Array.prototype.push.apply
: 2,193,469 ops/secarray.push.apply(array, concatArr);
Array.prototype.concat
: 22,701 ops/secarray = array.concat(concatArr);
LinkedList.prototype.concat
: 90,313,485 次操作/秒list.concat(concatList); // This function has to change only 1-2 refences
Array.prototype.push
在for 循环中:3,794,962 次操作/秒for (var i = 0, len = concatArr.length; i < len; i++) { array.push(concatArr[i]); } // Probably fastest in real life
Array.prototype.push.apply
: 2,193,469 次操作/秒array.push.apply(array, concatArr);
Array.prototype.concat
: 22,701 次操作/秒array = array.concat(concatArr);
Unfortunately, the LinkedList
version doesn't work if you want to concat an array/list to multipleLinkedList
s. It also has no benefit if an array has to be copied to a LinkedList
before every concat operation. So, for me, the winner is the for loop.
不幸的是,LinkedList
如果您想将一个数组/列表连接到多个LinkedList
s ,则该版本不起作用。如果必须LinkedList
在每次 concat 操作之前将数组复制到 a ,它也没有任何好处。所以,对我来说,赢家是for 循环。
One reason to not use Array.prototype.push.apply
is that it fails if the concatenated array is too big. According to this answer, the concatenated array can't have more than 500,000 elements in Firefox and 150,000 elements in Chrome.
不使用的Array.prototype.push.apply
原因之一是如果连接的数组太大,它就会失败。根据这个答案,连接数组在 Firefox 中不能超过 500,000 个元素,在 Chrome 中不能超过 150,000 个元素。
I excluded the spread operatorbecause it is not supported by all browsers. In Chrome, it's about as fast as Array.prototype.push.apply
, in Firefox it is a bit slower.
我排除了传播运算符,因为并非所有浏览器都支持它。在 Chrome 中,它与 一样快Array.prototype.push.apply
,在 Firefox 中它会慢一点。