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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:21:25  来源:igfitidea点击:

Javascript: push an entire list?

javascript

提问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

pushwill work, but you also need to use apply.

push会起作用,但您还需要使用apply.

var a = [1,2,3];
a.push.apply(a, [4,5])

回答by Salvador Dali

var list = [1, 2];

People already showed you how to do this with:

人们已经向您展示了如何做到这一点:

  1. push: list.push.apply(list, [3, 4]);
  2. concat: list = list.concat([4, 5]);
  1. 推: list.push.apply(list, [3, 4]);
  2. 连接: 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/sec

    list.concat(concatList);
    // This function has to change only 1-2 refences
    
  • Array.prototype.pushin a for loop: 3,794,962 ops/sec

    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 ops/sec

    array.push.apply(array, concatArr);
    
  • Array.prototype.concat: 22,701 ops/sec

    array = array.concat(concatArr);
    
  • LinkedList.prototype.concat: 90,313,485 次操作/秒

    list.concat(concatList);
    // This function has to change only 1-2 refences
    
  • Array.prototype.pushfor 循环中: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 LinkedListversion doesn't work if you want to concat an array/list to multipleLinkedLists. It also has no benefit if an array has to be copied to a LinkedListbefore every concat operation. So, for me, the winner is the for loop.

不幸的是,LinkedList如果您想将一个数组/列表连接到多个LinkedLists ,则该版本不起作用。如果必须LinkedList在每次 concat 操作之前将数组复制到 a ,它也没有任何好处。所以,对我来说,赢家是for 循环

One reason to not use Array.prototype.push.applyis 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 中它会慢一点。