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.

2

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

The size of the object you return is incorrect, as you're not doing a check for hasOwnProperty. See my answer.
You could use i<array1.length or i<array2.length instead of i<3 in the for loop. Hopefully, when you use this the arrays are of the same length (I suppose you cant assume that the arrays are the same length but they should be since you're converting them to an object)
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.

2 Comments

Well, no, technically his method will return an Array, which does have a length property. That length will always be 0, though.
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. =]
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.

2 Comments

what you mean by "hard-coding"? I am unfamiliar with that term
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.