-2
I have an array of objects and want to leave it empty by removing all its elements. How do I ?
let arr = [1, 2, 3, 4, 5];
-2
I have an array of objects and want to leave it empty by removing all its elements. How do I ?
let arr = [1, 2, 3, 4, 5];
8
Regardless of the values of an array, they are valid methods to empty an array arr
:
arr = [];
This code will reset the variable arr
for a new empty array. Note that the same thing can also be done if it is set in an object property.
It is ideal if you have no other references to the original array arr
, since that nay modifies the original array, only replaces it with a new, empty one.
It is important to be careful with this alternative given that if you have references to the array arr
in other parts of your code, they will remain unchanged. In short, use this alternative only when you are not keeping other references to the original array - which is not, in fact, being emptied, just replaced in one (and not all) given location.
This is the fastest alternative.
And this code shows the reference problem if due care is not taken:
let arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
let arr2 = arr1; // Referência à `arr1` em outra variável.
// Note que estamos apenas substituindo o valor da variável `arr1`.
// Não estamos, de fato, apagando nenhum dos elementos do array.
arr1 = [];
console.log(arr2); //=> ['a', 'b', 'c', 'd', 'e', 'f']
Note also that if the array has been declared with const
, cannot use this alternative. However, modifications to the array itself are still valid as explained here and here.
All of the following solutions will modify the original array (and therefore reflect changes in all references).
length
arr.length = 0;
This will clear the existing array by setting its property length
to zero. Basically, the property length
of arrays is a Setter which, when changed, is able to modify the array to suit the new value.
Array.prototype.splice
arr.splice(0, arr.length);
Use the method Array.prototype.splice
will work perfectly to remove all items from arr
. However, as this method returns an array with all items removed, will eventually return a copy of the original array (since all have been removed). However, benchmarks demonstrate that this does not imply any additional cost in performance.
Array.prototype.pop
until emptying the arraywhile (arr.length) {
arr.pop();
}
This solution uses the Array.prototype.pop
to remove the last element of the array. Loop is used while
to repeat the removal of the final elements until the array has been completely emptied.
This solution is not very succinct and is also the least performative of all presented.
Of all the alternatives presented above, 1 is the fastest, since it does not delete the array itself. Only modifies the variable that contained it with a new empty array.
However, when deleting elements from the original array is really necessary, alternatives 2 and 3 are very similar in performance and are significantly superior to alternative 4. For results, refer to this benchmark.
This is an adaptation of this excellent collective effort (response) Stack Overflow in English.
Thanks for the help Luiz.
Browser other questions tagged javascript array
You are not signed in. Login or sign up in order to post.
It’s much easier and faster you assign
[]
to the variable that contained the list of objects. If you want to delete one by one, you can loop: while (matrix.length > 0) matrix.shift();– epx
You can only assign an empty array, as Exp said, or use the method
shift
thus:array.shift(0, array.length)
.– JeanExtreme002
as @epx commented,
nome-da-variavel = []
ornome-da-variavel.length = 0
– Ricardo Pontual
@Jeanextreme002, the method
Array.prototype.shift
has no such arguments.– Luiz Felipe
Thank you guys for your help
– user222623
@Luizfelipe Oops, was
splice
. I got confused at the time, sorry. Correction:array.splice(0, array.length)
– JeanExtreme002