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?
Is there a way to empty an array and if so possibly with For instance,
How can I empty that? |
|||||||||
|
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. (most of the comments on this answer are about method 1, which was my original answer) Ways to clear an existing array Method 1 (this was my original answer to the question)
This code will set the variable This is also the fastest solution. Method 2 (as suggested by Matthew Crumley)
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)
Using Method 4 (as suggested by tanguy_k)
This solution is not very succinct but it is by far the fastest solution (apart from setting the array to a new array). If you care about performance, consider using this method to clear an array. Benchmarks show that this is at least 10 times faster than setting the length to 0 or using splice(). |
|||||||||||||||||||||
|
If you need to keep the original array because you have other references to it that should be updated too, you can clear it without creating a new array by setting its length to zero:
|
|||||||||||||||||||||
|
Here the fastest working implementation while keeping the same array:
Or as an Underscore.js mixin:
And the test that goes with it:
Here the updated jsPerf: http://jsperf.com/array-destroy/32 Answers from Jan and David McCurley are incorrect. As noted by 755 in a comment, the algorithm provided by Jan only removes half of the items from the array. |
|||||||||||||||||||||
|
A more cross-browser friendly and more optimal solution will be to use the
|
|||||||||||||||||||||
|
You can add this to your JavaScript file to allow your arrays to be "cleared":
|
|||||||||
|
and call it: =) |
|||||||||||||
|
How about the below modified version of Jan's initial suggestion?
|
|||
|
In case you are interested in the memory allocation, you may compare each approach using something like this jsfiddle in conjunction with chrome dev tools' timeline tab. You will want to use the trash bin icon at the bottom to force a garbage collection after 'clearing' the array. This should give you a more definite answer for the browser of your choice. A lot of answers here are old and I wouldn't rely on them but rather test as in @tanguy_k's answer above. (for an intro to the aforementioned tab you can check out here) Stackoverflow forces me to copy the jsfiddle so here it is:
And you should take note that it may depend on the type of the array elements, as javascript manages strings differently than other primitive types, not to mention arrays of objects. The type may affect what happens. |
|||
|
The simplest way to delete all content of Array is to set length to zero. Eg. var myArray = ['A','B','C'];
|
|||||
|
I just did this on some code I am working on. It cleared the array. |
||||
|
or
or
|
|||||
|
Will delete the Array then simply re-create it.
|
|||||
|
The fastest solution for all current browsers is to implement the
Or you could just use a while loop:
But the best answer as found above would be to use the
|
|||||||||
|