user818700
user818700

Reputation:

Struggling with JS Array Lengths

I have this piece of code, when you look at it it would seem that the logic should run as follows:

I create 3 empty arrays, I add one value to bankSelectedData, I print out the values, obviously bankSelectedData is now 1 and bankSelectionArrayCurrent length is 0, but then I say:

bankSelectionArrayPrevious = bankSelectionArrayCurrent;

So I'm turning bankSelectionArrayPrevious into bankSelectionArrayCurrent. When I check the size of bankSelectionArrayPrevious is says '1' ?? How is this possible?

var bankSelectionArrayCurrent = new Array();
var bankSelectionArrayPrevious = new Array();
var bankSelectedData = new Array();
    bankSelectedData.push("value1");

alert("bankSelectionArrayCurrent length: "+bankSelectionArrayCurrent.length);
alert("bankSelectedData length: "+bankSelectedData.length);

if(bankSelectedData.length != bankSelectionArrayCurrent.length){
    bankSelectionArrayPrevious = bankSelectionArrayCurrent;
    bankSelectionArrayCurrent.length = 0

    alert("previousSize: "+bankSelectedData.length);
    alert("currentSize: "+bankSelectionArrayCurrent.length);
}​

Thanks for any advice!

Upvotes: 0

Views: 95

Answers (3)

Flash
Flash

Reputation: 16703

Where you have

alert("previousSize: "+bankSelectedData.length);

perhaps you meant:

alert("previousSize: "+bankSelectionArrayPrevious.length);

Upvotes: 2

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

Your alert is wrong. It should be:

alert("previousSize: "+bankSelectionArrayPrevious.length);

Upvotes: 2

Pointy
Pointy

Reputation: 413720

Your code never changes "bankSelectedData", so its length remains 1 after you add that first value.

Your code does not contain any alert() call with the length of "bankSelectionArrayPrevious".

Upvotes: 2

Related Questions