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? |
|||||
locked by Madara Uchiha♦ Aug 30 at 13:11This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here |
|||||
|
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. This code sample shows the issue you can encounter when using this method:
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, 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. 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. 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. If you vote for this answer, please upvote the other answers that I have referenced as well. |
|||||||||||||||||||||
|
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:
|
|||||||||||||||||||||
|
FYI Map defines Or as an Underscore.js mixin:
Or a simple function:
FYI it cannot be simplified to And the tests that goes with it:
Here the updated jsPerf: |
|||||||||||||||||||||
|
A more cross-browser friendly and more optimal solution will be to use the
|
|||||||||||||||||||||
|
Performance test: http://jsperf.com/array-clear-methods/3
|
|||||||||||||||||||||
|
The answers that have no less that 2739 upvotes by now are misleading and incorrect. The question is: "How do you empty your existing array?" E.g. for
Let's say your mother asks you to empty the trash can.
Emptying an array object is the easiest thing ever:
This way, the can under "A" is not only empty, but also as clean as new!
No, you were asked to empty it:
This is the only code that correctly empties the contents of a given JavaScript array. |
|||||||||||||||||||||
|
You can add this to your JavaScript file to allow your arrays to be "cleared":
Then you can use it like this:
Or if you want to be sure you don't destroy something:
Lots of people think you shouldn't modify native objects (like Array), and I'm inclined to agree. Please use caution in deciding how to handle this. |
|||||||||||||||||
|
and call it: =) |
|||||||||||||||||||||
|
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. |
|||
|
There is a lot of confusion and misinformation regarding the while;pop/shift performance both in answers and comments. The while/pop solution has (as expected) the worst performance. What's actually happening is that setup runs only once for each sample that runs the snippet in a loop. eg:
I have created a new test that works correctly : http://jsperf.com/empty-javascript-array-redux Warning: even in this version of the test you can't actually see the real difference because cloning the array takes up most of the test time. It still shows that |
|||||||||
|
I just did this on some code I am working on. It cleared the array. |
|||||
|
Use a modified version of Jan's initial suggestion:
|
|||||
|
If you are using
or
then you are just creating new array which points to specific memory location. This means previous array will be remain in memory till garbage collection. So its not the better way to use. Instead of these two solutions are better.
and
As per previous answer by kenshou.html, second method is faster. |
|||||||||||||||||||||
|
If you use constants then you have no choice:
You can not reasign:
You can only truncate:
|
|||
|
You can easily create a function to do that for you, change the length or even add it to native Array as remove() function for reuse. Imagine you have this array:
OK, just simply run this:
and the result is:
easy way to empty an array... Also using loop which is not necessary but just another way to do that:
There are also tricky way which you can think about, for example something like this:
So if arr has 5 items, it will splice 5 items from 0, which means nothing will remain in the array. Also other ways like simply reassign the array for example:
If you look at the Array functions, there are many other ways to do this, but the most recommended one could be changing the length. As I said in the first place, you can also prototype remove() as it's the answer to your question. you can simply choose one of the methods above and prototype it to Array object in JavaScript, something like:
and you can simply call it like this to empty any array in your javascript application:
|
||||
|
To Empty a Current memory location of an array use:
Example:
Use Empty Array with new Memory address use : slice(0, 0)
To assigning new empty array
|
|||||||||
|
I'm surprised no one has suggested this yet:
This yields an array in quite a different state from the other solutions. In a sense, the array has been 'emptied':
You can produce an equivalent array with |
|||||
|
Use below if you need to empty Angular 2+ FormArray.
|
|||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?