views:

42

answers:

4

Hello all,

I have a javascript that has custom indexes, I created them like so:

var rand = event.timeStamp; //jquery on click event object

freeze_array[rand] = month + ',' + model_name + ',' + activity;

To remove the above element I have this:

freeze_array.splice(rand, 1); 

But this does not remove the element as I can see it in my firebug dom object viewer. Here is an example of the array:

My indexes are in the form: 1283519490632 - too long to be an integer that is required by the splice method?

Thanks all for any help

A: 

Try this:

delete freeze_array[ rand ];
dionyziz
This leaves a "hole" in the array, its not a complete removal.
Abs
+1  A: 

As you mentioned, the index argument must be an integer. Maybe you can use an object that holds indices as follows:

var lastIndex=0; // that shall be global...
var pointer = {};

....

pointer[rand] = lastIndex;
++lastIndex;

Then use it as follows:

freeze_array = freeze_array.splice(pointer[rand], 1); 
Zafer
I have stopped using a large number and it works. Your method is good.
Abs
A: 

Yes the index must be an integer. Your value is too large for a integer. See at w3schools

index: Required. An integer that specifies at what position to add/remove elements

PoweRoy
A: 

It's a bad idea to use an Array for this. Example:

var arr  = [];
arr[444] = "foo";
alert(arr.length);

that would alert us 443. So if you use an array, Javascript will create 444 fields within this array, in your case way more. You should use an array like object for this:

var obj  = {};
arr[444] = "foo";

and then use delete arr[444] to remove that key.

jAndy