I want to bind two regular array into one associative array.
The values of the first are the keys and the values of the second are the elements.
var array1=new Array("key1","Key2","Key3");
var array2=new Array("Value1","Value2","Value3");
var associative_array=new Array();
for(var i=0;i<3;i++){
associative_array[array1[i]]=array2[i];
}
But when i try to get the length of the new associative array, i noticed it’s empty:
alert(associative_array.length);//always 0
What am doing wrong please? Thanx in advance.
In Javascript, use Objects for associative arrays. There are many ways to rewrite this, but using your example simply replace “new Array()” with “new Object()”:
You’ll need to implement something to get the size of the object. The following should be part of a function or method, but should illustrate the idea: