2

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.

Luca
  • 20,399
  • 18
  • 49
  • 70
  • 2
    Possible duplicate of [Why does a string index in a javascript array not increase the length size?](http://stackoverflow.com/questions/9526860/why-does-a-string-index-in-a-javascript-array-not-increase-the-length-size). Long story short: there are no "associative arrays" in Javascript. – deceze Apr 24 '12 at 02:15
  • 1
    Include at the end: `associative_array.length = i;` – RobG Apr 24 '12 at 03:47

3 Answers3

6

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()":

var array1=new Array("key1","Key2","Key3");
var array2=new Array("Value1","Value2","Value3");

var associative_array=new Object();
for(var i=0;i<3;i++){
  associative_array[array1[i]]=array2[i];
}

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:

var o_size = 0;
for (key in associative_array) {
  o_size++;
}
alert(o_size);
andrew
  • 76
  • 2
3

The way I'd do it:

function createAssociativeArray(arr1, arr2) {
    var arr = {};
    for(var i = 0, ii = arr1.length; i<ii; i++) {
        arr[arr1[i]] = arr2[i];
    }
    return arr;
}

var array1 = ["key1", "Key2", "Key3"];
var array2 = ["Value1", "Value2", "Value3"];
var associativeArray = createAssociativeArray(array1, array2);

This (unlike your method) will return an Object, which does not have a length property, as each of its values is not treated as an index, but rather a property. If you desperately needed to get the length of the properties of an object, you could do something like this:

function getObjectPropertiesLength(obj) {
    var propNum = 0;
    for(prop in obj) {
        if(obj.hasOwnProperty(prop)) propNum++;
    }
    return propNum;
}

var values = getObjectPropertiesLength(associativeArray);

Note that if you add functions to an object, these will be treated as properties of the object and thus will contribute to the object's properties length.

What your method does:

The reason your method fails is that you're adding properties to an Array object. Yes, arrays are technically objects in Javascript, which means you can assign properties to them. This isn't using arrays in the way they are intended, though, which can have unexpected results. One of those results is that the length value will suddenly fail to work.

This is because Array objects expect to have their properties indexed with numbers. If you assign a property to an object with a string, the function that returns the Array's length effectively can't see that property, so it won't contribute to the length of the array.

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • 1
    Well, no, technically his method will return an Array, which *does* have a length property. That length will always be 0, though. – kojiro Apr 24 '12 at 02:19
  • Oops, right, because Arrays are objects and he can assign properties to objects but the array length function won't see those etc etc. My mistake. =] – Elliot Bonneville Apr 24 '12 at 02:23
1

In JavaScript, an associative array is just an object. It does not have a length property. In fact, it's not a great idea to use an Array() as an associative array. Your code for generating the object is otherwise OK, except maybe for hard-coding the size at 3.

kojiro
  • 74,557
  • 19
  • 143
  • 201
  • what you mean by "hard-coding"? I am unfamiliar with that term – ajax333221 Apr 24 '12 at 02:26
  • @ajax333221: He means that it's not a dynamic value, e.g. it will always be 3 no matter what the content of the array. It's really irrelevant, though, as this is a simple test case. – Elliot Bonneville Apr 24 '12 at 02:29