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?

2

4 Answers 4

11

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

Or just Passengers.length = 0;

Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

3

Very Simple

riz = [];

or

riz.length = 0;

1 Comment

Too late for the party...
2

You can just re-assign a new Array instance

Passengers = [ ];

respectively

Passengers = new Array();

The garbage collector till take care of the rest.

3 Comments

thanks a lot. Passengers = [ ]; Passengers = new Array(); both solves the problem
Yeah... only both applied solves it :D @jAndy You see how multiple alternatives can help the OP to understand the things? :)
:) each one is ok thanks again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.