Reputation: 11
I am using:
var myArry = [];
as a global variable. I then populate the array with some data from the database. If I then want to replace the array data with new data do I have to empty / reset the original array.
Or what is the correct method?
Upvotes: 0
Views: 87
Reputation: 3384
Here are some ways to empty an array:
myArray = [];
myArray = new Array();
myArray.length = 0;
myArray.splice(0, myArray.length);
These will all work.
Upvotes: 4
Reputation: 782
If it comes from the database, it would be better to clear your array since some data may be deleted.
To do so, you can write myArray = [];
Upvotes: 1
Reputation: 32586
myArray.length = 0;
will empty the array. You can then repopulate it as needed.
Upvotes: 2