简体   繁体   中英

create an associative array from two regular arrays

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

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);

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM