11

my script creates an empty array and then fill it. But if new arguments come then script is expected to destroy old one and create new one.

var Passengers = new Array();

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

I wanna destroy the old one because new count may be less than old one and last elements of array still will store old array? is that right and if it is how can I destroy it?

ismail atkurt
  • 139
  • 1
  • 5
  • This post will help you. http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript – Riz Apr 26 '13 at 14:19
  • this post will help you. http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript – Riz Apr 26 '13 at 14:20

4 Answers4

11

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

Or just Passengers.length = 0;

markzzz
  • 47,390
  • 120
  • 299
  • 507
7

You can simply do this to empty an array (without changing the array object itself):

Passengers.length = 0;

Or, with your code:

function FillPassengers(count){
    for(var i=0;i<count;i++)
        Passengers[i] = i;
    Passengers.length = count;
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
3

Very Simple

riz = [];

or

riz.length = 0;
Mark Coleman
  • 40,542
  • 9
  • 81
  • 101
Riz
  • 123
  • 1
  • 3
2

You can just re-assign a new Array instance

Passengers = [ ];

respectively

Passengers = new Array();

The garbage collector till take care of the rest.

jAndy
  • 231,737
  • 57
  • 305
  • 359