Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中清空数组?(How do I empty an array in JavaScript?)

Is there a way to empty an array and if so possibly with .remove() ?(有没有一种方法可以清空数组,如果可以的话,可以使用.remove()吗?)

For instance,(例如,) A = [1,2,3,4]; How can I empty that?(我该如何清空?)   ask by akano1 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ways to clear an existing array A :(清除现有数组A :)

Method 1(方法1) (this was my original answer to the question)((这是我对问题的原始回答)) A = []; This code will set the variable A to a new empty array.(此代码会将变量A设置为新的空数组。) 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.(如果您在其他任何地方都没有引用原始数组A则这是完美的选择,因为这实际上会创建一个全新的(空)数组。) 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 .(仅当仅通过其原始变量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 )(方法2 (由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.(这将通过将其长度设置为0来清除现有数组。有人认为这可能不适用于JavaScript的所有实现,但事实并非如此。) It also works when using "strict mode" in ECMAScript 5 because the length property of an array is a read/write property.(当在ECMAScript 5中使用“严格模式”时,它也可以工作,因为数组的length属性是读/写属性。) Method 3 (as suggested by Anthony )(方法3 (由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.(使用.splice()可以完美地工作,但是由于.splice()函数将返回包含所有已删除项目的数组,因此它实际上将返回原始数组的副本。) Benchmarks suggest that this has no effect on performance whatsoever.(基准表明,这对性能没有任何影响。) Method 4 (as suggested by tanguy_k )(方法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.(该解决方案不是很简洁,它也是最慢的解决方案,与原始答案中引用的早期基准相反。) 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 .(在清除现有数组的所有方法中,方法2和3在性能上非常相似,并且比方法4快得多。请参阅此基准测试 。) 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.(正如Diadistis在下面的回答中所指出的那样,用于确定上述四种方法的性能的原始基准存在缺陷。) 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 .(以下基准测试可修复此缺陷: 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).(它清楚地表明方法#2(长度属性)和#3(拼接)是最快的(不计算不会改变原始数组的方法#1)。) 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.(如果您对此答案投赞成票,请同时投票赞成我引用的其他答案。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...