javascript动态填充关联数组并获取值

时间:2022-07-08 16:00:36

I want to build an associative array based on an array and then get the values of that associative array. The structure of the associative array is as follows:

我想基于数组构建一个关联数组,然后获取该关联数组的值。关联数组的结构如下:

        var myAssociativeArr = new Array();

        myAssociativeArr = [
         { id:'1',
          lname:'doe',
          fname:'john' 
        },
         { id:'2',
          lname:'smith',
          fname:'john' 
        }

        ]

I have three arrays of same length from which i will build this associative array i.e. id, lname and fname array.

我有三个相同长度的数组,我将从中构建这个关联数组,即id,lname和fname数组。

    for(var i=0; idArray.length;i++)
    {
    myAssociativeArr [id]=idArray[i];
    myAssociativeArr [lname]=lnameArray[i];
    myAssociativeArr [fname]=fnameArray[i];
    }

Please tell if the above approach is correct, if not how can build this array and also how can i get the valeus of this array via loop.

请告诉我上面的方法是否正确,如果没有,如何构建这个数组,以及如何通过循环获取该数组的valeus。

Your help will be appreciated.

我们将不胜感激。

3 个解决方案

#1


18  

You are very close. First of all, if you wish to use the array subscript notation, you have to pass the keys themselves (strings in your case, like this):

你很近。首先,如果你想使用数组下标表示法,你必须自己传递键(在你的情况下是字符串,如下所示):

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    var newElement = {};
    newElement['id'] = idArray[i];
    newElement['lname'] = lnameArray[i];
    newElement['fname'] = fnameArray[i];
    myAssociativeArr.push(newElement);
}

Where the key names are known strings, it's often preferable to use the completely equivalent notation of object properties:

如果键名是已知字符串,则通常最好使用完全等效的对象属性表示法:

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    var newElement = {};
    newElement.id = idArray[i];
    newElement.lname = lnameArray[i];
    newElement.fname = fnameArray[i];
    myAssociativeArr.push(newElement);
}

You can be even more concise by using object literals, as you did in your sample output:

通过使用对象文字,您可以更加简洁,就像在示例输出中所做的那样:

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    myAssociativeArr.push({
        id: idArray[i],
        lname: lnameArray[i],
        fname: fnameArray[i]
    });
}

Edit: fixed loop indexing to not be infinite.

编辑:固定循环索引不是无限的。

You read elements the same way you write them: myAssociativeArr[i]['id'] etc., or myAssociativeArr[i].id etc.

您以与编写元素相同的方式读取元素:myAssociativeArr [i] ['id']等,或myAssociativeArr [i] .id等。

For lookups by ID, it's a good idea to construct an object for this.

对于按ID查找,最好为此构造一个对象。

var myObject = {};
for (var i=0; i < idArray.length; i++) {
    myObject[idArray[i]] = {
        id: idArray[i],
        lname: lnameArray[i],
        fname: fnameArray[i]
    };
}

To look up:

去查查看:

myObject['2'] // => { id: '2', ... }

#2


4  

not quite, try this:

不太好,试试这个:

 for(var i=0; idArray.length; i++)
 {
    myAssociativeArr[i] = {
                           id: idArray[i], 
                           lname: lnameArray[i], 
                           fname: fnameArray[i]
                          };
 }

to get the id of the 5th element: myAossociativeArr[i]['id'], I'm sure you can figure out the rest from here ;)

得到第5个元素的id:myAossociativeArr [i] ['id'],我相信你可以从这里找出其余部分;)

#3


1  

for(var i=0; idArray.length;i++)
    {
    myAssociativeArr [i][id]=idArray[i];
    myAssociativeArr [i][lname]=lnameArray[i];
    myAssociativeArr [i][fname]=fnameArray[i];
    }

#1


18  

You are very close. First of all, if you wish to use the array subscript notation, you have to pass the keys themselves (strings in your case, like this):

你很近。首先,如果你想使用数组下标表示法,你必须自己传递键(在你的情况下是字符串,如下所示):

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    var newElement = {};
    newElement['id'] = idArray[i];
    newElement['lname'] = lnameArray[i];
    newElement['fname'] = fnameArray[i];
    myAssociativeArr.push(newElement);
}

Where the key names are known strings, it's often preferable to use the completely equivalent notation of object properties:

如果键名是已知字符串,则通常最好使用完全等效的对象属性表示法:

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    var newElement = {};
    newElement.id = idArray[i];
    newElement.lname = lnameArray[i];
    newElement.fname = fnameArray[i];
    myAssociativeArr.push(newElement);
}

You can be even more concise by using object literals, as you did in your sample output:

通过使用对象文字,您可以更加简洁,就像在示例输出中所做的那样:

var myAssociativeArr = [];
for (var i=0; i < idArray.length; i++) {
    myAssociativeArr.push({
        id: idArray[i],
        lname: lnameArray[i],
        fname: fnameArray[i]
    });
}

Edit: fixed loop indexing to not be infinite.

编辑:固定循环索引不是无限的。

You read elements the same way you write them: myAssociativeArr[i]['id'] etc., or myAssociativeArr[i].id etc.

您以与编写元素相同的方式读取元素:myAssociativeArr [i] ['id']等,或myAssociativeArr [i] .id等。

For lookups by ID, it's a good idea to construct an object for this.

对于按ID查找,最好为此构造一个对象。

var myObject = {};
for (var i=0; i < idArray.length; i++) {
    myObject[idArray[i]] = {
        id: idArray[i],
        lname: lnameArray[i],
        fname: fnameArray[i]
    };
}

To look up:

去查查看:

myObject['2'] // => { id: '2', ... }

#2


4  

not quite, try this:

不太好,试试这个:

 for(var i=0; idArray.length; i++)
 {
    myAssociativeArr[i] = {
                           id: idArray[i], 
                           lname: lnameArray[i], 
                           fname: fnameArray[i]
                          };
 }

to get the id of the 5th element: myAossociativeArr[i]['id'], I'm sure you can figure out the rest from here ;)

得到第5个元素的id:myAossociativeArr [i] ['id'],我相信你可以从这里找出其余部分;)

#3


1  

for(var i=0; idArray.length;i++)
    {
    myAssociativeArr [i][id]=idArray[i];
    myAssociativeArr [i][lname]=lnameArray[i];
    myAssociativeArr [i][fname]=fnameArray[i];
    }