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

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

问题描述

有没有办法为空数组,如果是的话可能与一个.remove()

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

例如,

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

我哪有空的?

推荐答案

方法来清除现有阵列 A

方法1

(这是我原来的回答这个问题)

(this was my original answer to the question)

A = [];

这code将设置变量 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.

这code示例说明你可以使用这种方法时遇到的问题:

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 (如建议通过的马修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.请参阅本的基准

正如 Diadistis 在他们的回答的下面,即用来确定上述是有缺陷的四种方法的性能的原始基准。原来的基准重用清除数组,以便在第二次迭代被清除的数组已经空了。

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://jsperf.com/array-destroy/151 。它清楚地表明,方法#2(长度属性)和#3(拼接)是最快的(不计算方法#1不改变原来的数组)。

The following benchmark fixes this flaw: http://jsperf.com/array-destroy/151. 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.

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

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