如何在 JavaScript 中清空数组? [英] How do I empty an array in JavaScript?

查看:19
本文介绍了如何在 JavaScript 中清空数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法清空数组,如果可以的话,可以用 .remove() ?

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

例如,

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

我怎样才能清空它?

推荐答案

清除现有数组的方法 A:

Ways to clear an existing array A:

方法一

(这是我对问题的原始回答)

(this was my original answer to the question)

A = [];

此代码将变量 A 设置为一个新的空数组.如果您在其他任何地方都没有 对原始数组 A 的引用,这是完美的,因为这实际上创建了一个全新的(空)数组.你应该小心使用这个方法,因为如果你从另一个变量或属性引用了这个数组,原始数组将保持不变.仅当您仅通过其原始变量 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']

方法 2(如 建议.com/users/2214/matthew-crumley">马修·克鲁姆利)

Method 2 (as suggested by Matthew Crumley)

A.length = 0

这将通过将其长度设置为 0 来清除现有数组.有人认为这可能不适用于 JavaScript 的所有实现,但事实证明并非如此.它也适用于使用严格模式"在 ECMAScript 5 中,因为数组的长度属性是读/写属性.

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.

方法 3(如 安东尼)

A.splice(0,A.length)

使用 .splice() 可以完美运行,但是由于 .splice() 函数将返回一个包含所有已删除项目的数组,它实际上会返回一个副本的原始数组.基准测试表明,这对性能没有任何影响.

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.

方法 4(由 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.

性能

在清除现有数组的所有方法中,方法 2 和方法 3 在性能上非常相似,并且比方法 4 快很多.请参阅此 基准.

正如 Didistis 在他们的 答案 在下面,用于确定上述四种方法的性能的原始基准存在缺陷.原始基准重用已清除的数组,因此第二次迭代是清除已经为空的数组.

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.

以下基准修复了此缺陷:http://jsben.ch/#/hyj65.它清楚地表明方法#2(长度属性)和#3(拼接)是最快的(不计算不改变原始数组的方法#1).

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.

这篇关于如何在 JavaScript 中清空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆