local-host 2009-08-05 09:08 采纳率: 100%
浏览 1877
已采纳

如何清空 JavaScript 中的数组?

Is there a way to empty an array and if so possibly with .remove()?

For instance,

A = [1,2,3,4];

How can I empty that?

转载于:https://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript

  • 写回答

17条回答 默认 最新

  • 程序go 2009-08-05 09:10
    关注

    Ways to clear an existing array A:

    Method 1

    (this was my original answer to the question)

    A = [];
    

    This code will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array. You should be careful with this method because if you have referenced this array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original variable A.

    This is also the fastest solution.

    This code sample shows the issue you can encounter when using this method:

    var arr1 = ['a','b','c','d','e','f'];
    var arr2 = arr1;  // Reference arr1 by another variable 
    arr1 = [];
    console.log(arr2); // Output ['a','b','c','d','e','f']
    

    Method 2 (as suggested by Matthew Crumley)

    A.length = 0
    

    This will clear the existing array by setting its length to 0. Some have argued that this may not work in all implementations of JavaScript, but it turns out that this is not the case. It also works when using "strict mode" in ECMAScript 5 because the length property of an array is a read/write property.

    Method 3 (as suggested by Anthony)

    A.splice(0,A.length)
    

    Using .splice() will work perfectly, but since the .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggest that this has no effect on performance whatsoever.

    Method 4 (as suggested by tanguy_k)

    while(A.length > 0) {
        A.pop();
    }
    

    This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer.

    Performance

    Of all the methods of clearing an existing array, methods 2 and 3 are very similar in performance and are a lot faster than method 4. See this benchmark.

    As pointed out by Diadistis in their answer below, the original benchmarks that were used to determine the performance of the four methods described above were flawed. The original benchmark reused the cleared array so the second iteration was clearing an array that was already empty.

    The following benchmark fixes this flaw: http://jsben.ch/#/hyj65. It clearly shows that methods #2 (length property) and #3 (splice) are the fastest (not counting method #1 which doesn't change the original array).


    This has been a hot topic and the cause of a lot of controversy. There are actually many correct answers and because this answer has been marked as the accepted answer for a very long time, I will include all of the methods here. If you vote for this answer, please upvote the other answers that I have referenced as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(16条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)