L
Laser Lips
/*I was messing about with accossiative arrays when I discovered that
an
accossiative array does not have a length.*/
var myAsso = [];
myAsso["one"]="one";
myAsso["two"]="two";
myAsso["three"]="three";
alert(myAsso.length); //=0
/*I see that arrays can have mixed key attributes of both
numbers(index) and strings (accossiative), but the array's length will
only be counted using the indexed based keys.*/
var myArr=[];
myArr[0]="my value";
myArr[1]="my value";
myArr[2]="my value";
myArr["3"]="my value"; //this one is still classed as an index
myArr["a"]="my value";
myArr["b"]="my value";
myArr["c"]="my value";
myArr["d"]="my value";
alert("Array length = " + myArr.length); //length = 4
//the following loop will pick of the index based keys
for(var x=0;x<myArr.length;x++)
{
alert("for x) "+x + " = "+myArr[x]);
}
//the following loop will pick of the accossiative based keys
for(var n in myArr)
{
alert("for in) "+n + " = " + myArr[n]);
}
//add an accossiative element
myArr[4]="my value";
alert("Array length = " + myArr.length);//length = 5
//able to add a function to the array.
myArr.test=function()
{
alert("Func");
}
myArr.test();
alert("Array length = " + myArr.length);//length = 5
Even though I can add functions to the array, the array is still
classed as an array because it has length attribute.
Why do the accossiative based keys not get counted?
Graham
an
accossiative array does not have a length.*/
var myAsso = [];
myAsso["one"]="one";
myAsso["two"]="two";
myAsso["three"]="three";
alert(myAsso.length); //=0
/*I see that arrays can have mixed key attributes of both
numbers(index) and strings (accossiative), but the array's length will
only be counted using the indexed based keys.*/
var myArr=[];
myArr[0]="my value";
myArr[1]="my value";
myArr[2]="my value";
myArr["3"]="my value"; //this one is still classed as an index
myArr["a"]="my value";
myArr["b"]="my value";
myArr["c"]="my value";
myArr["d"]="my value";
alert("Array length = " + myArr.length); //length = 4
//the following loop will pick of the index based keys
for(var x=0;x<myArr.length;x++)
{
alert("for x) "+x + " = "+myArr[x]);
}
//the following loop will pick of the accossiative based keys
for(var n in myArr)
{
alert("for in) "+n + " = " + myArr[n]);
}
//add an accossiative element
myArr[4]="my value";
alert("Array length = " + myArr.length);//length = 5
//able to add a function to the array.
myArr.test=function()
{
alert("Func");
}
myArr.test();
alert("Array length = " + myArr.length);//length = 5
Even though I can add functions to the array, the array is still
classed as an array because it has length attribute.
Why do the accossiative based keys not get counted?
Graham