Is there a way to destroy all elements of an array with a single command?

my script creates an empty array and then populates it. But if new arguments appear, it is expected that the script will destroy the old and create a new one.

var Passengers = new Array(); function FillPassengers(count){ for(var i=0;i<count;i++) Passengers[i] = i; } 

I want to destroy the old one, because the new account may be smaller than the old one, and the last elements of the array will still store the old array? is that right, and if so, can i destroy it?

+7
source share
4 answers

This will create a new empty Passengers = [] array. Not sure what you should do.

Or just Passengers.length = 0;

+8
source

You can simply do this to clear the array (without changing the array itself):

 Passengers.length = 0; 

Or with the code:

 function FillPassengers(count){ for(var i=0;i<count;i++) Passengers[i] = i; Passengers.length = count; } 
+5
source

You can just assign a new instance of Array

 Passengers = [ ]; 

respectively

 Passengers = new Array(); 

Garbage collector before caring for the rest.

+2
source

Very simple

 riz = []; 

or

 riz.length = 0; 
+2
source

All Articles