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? |
||||
add comment |
Very simple:
EDIT A little extra explanation is required here. The code above However (as other have pointed out below) if you have references to this array, you can empty the original array by setting its length to 0 or by calling Setting the length to zero is the most efficient solution, but this could create problems in some implementations of Javascript (although I don't know of any). Using So the preferred way to clear an existing array is:
(Matthew Crumley's answer to this question is probably the best one) |
|||||||||||||||||||||
|
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: =) |
|||||||||||||
|
The simplest way to delete all content of Array is to set length to zero. Eg. var myArray = ['A','B','C'];
|
|||||
|
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. |
|||
|
or
or
|
|||||
|
I just did this on some code I am working on. It cleared the array. |
||||
|
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
|
|||||||||
|
A = []
method before finding this thread, but I almost continued doing my normal thing until I saw @Daniel's highly rated comment on the currently accepted answer – phatskat Jan 19 '13 at 21:01