简体   繁体   English

从两个常规数组创建一个关联数组

[英]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. Thanx提前。

In Javascript, use Objects for associative arrays. 在Javascript中,将对象用于关联数组。 There are many ways to rewrite this, but using your example simply replace "new Array()" with "new Object()": 有很多方法可以重写它,但是使用你的例子只需将“new Array()”替换为“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. 这(与您的方法不同)将返回一个没有length属性的Object,因为它的每个值都不被视为索引,而是一个属性。 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. 您的方法失败的原因是您要向Array对象添加属性。 Yes, arrays are technically objects in Javascript, which means you can assign properties to them. 是的,数组在技术上是Javascript中的对象,这意味着您可以为它们分配属性。 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. 其中一个结果是length值突然失效。

This is because Array objects expect to have their properties indexed with numbers. 这是因为Array对象希望它们的属性用数字索引。 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. 如果为具有字符串的对象分配属性,则有效返回Array长度的函数无法看到该属性,因此它不会影响数组的长度。

In JavaScript, an associative array is just an object. 在JavaScript中,关联数组只是一个对象。 It does not have a length property. 它没有长度属性。 In fact, it's not a great idea to use an Array() as an associative array. 事实上,使用Array()作为关联数组并不是一个好主意。 Your code for generating the object is otherwise OK, except maybe for hard-coding the size at 3. 您生成对象的代码是正常的,除了可能将大小硬编码为3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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