user1223912
user1223912

Reputation: 11

javascript re-populate an array

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

Answers (3)

bitoffdev
bitoffdev

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

Masadow
Masadow

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

Andrew Cooper
Andrew Cooper

Reputation: 32586

myArray.length = 0; will empty the array. You can then repopulate it as needed.

Upvotes: 2

Related Questions