javascript 循环从二维数组在Javascript中创建对象键和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17353505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:59:54  来源:igfitidea点击:

Looping to create object Keys and Values in Javascript from 2D arrays

javascriptarraysobject

提问by user2494584

I have two arrays, one which holds the keys and one which holds arrays, each array containing values. I would like to make an array of objects, where each object pairs the keys and values. To do this, I created an array, and I am now trying to create and fill objects before pushing them into the array. My code looks similar to this:

我有两个数组,一个保存键,一个保存数组,每个数组包含值。我想创建一个对象数组,其中每个对象都将键和值配对。为此,我创建了一个数组,现在我尝试在将对象推入数组之前创建和填充对象。我的代码看起来类似于:

var keys = [key1, key2, key3];
var values = [
                [A-value1, A-value2, A-value3],
                [B-value1, B-value2, B-value3],
                [C-value1, C-value2, C-value3]
             ];

var arrayOfObjecs = [];
for(var i=0; i<values.length; i++){
    var obj = {
    for(var j=0; j<values[i].length; j++){
            keys[j] : values[i][j];
    }
    };
    arrayOfObjects.push(obj);
}

In the end, I would like for my arrayOfObjects to look like this:

最后,我希望我的 arrayOfObjects 看起来像这样:

var arrayOfObjects = [
                        {
                         key1 : A-value1,
                         key2 : A-value2,
                         key3 : A-value3
                        },
                        {
                         key1 : B-value1,
                         key2 : B-value2,
                         key3 : B-value3
                        },
                        {
                         key1 : C-value1,
                         key2 : C-value2,
                         key3 : C-value3
                        }
                     ];

This questionis similar to what I want to do, yet it won't allow me to loop a second time within the object.

这个问题与我想要做的类似,但它不允许我在对象内再次循环。

回答by GameAlchemist

Your question is in fact about the object properties square bracket notation :
using object[propertyname] is the same as using object.propertyname.

您的问题实际上是关于对象属性的方括号表示法:
使用 object[propertyname] 与使用 object 相同。属性名

var myObj  = {};
myObj['x'] = 12;
console.log(myObj.x);  -->> prints 12

Now in your code :

现在在你的代码中:

var arrayOfObjects = [];
for(var i=0; i<values.length; i++){
    var obj = {};
    for(var j=0; j<values[i].length; j++){
         obj[keys[j]] = values[i][j];  
      }
    arrayOfObjects.push(obj);
}

In reply to 'it does weird things' : this code works.

回复“它做了奇怪的事情”:此代码有效。

with this input :

使用此输入:

var keys = ['key1', 'key2', 'key3'];
var values = [
            [12,112, 1112],
            [31, 331, 3331],
            [64, 65, 66]
         ];

the output is :

输出是:

   {   {key1: 12, key2: 112, key3: 1112},  
       {key1: 31, key2: 331, key3: 3331},   
       {key1: 64, key2: 65, key3: 66}        }

fiddle is here : http://jsfiddle.net/fyt8A/

小提琴在这里:http: //jsfiddle.net/fyt8A/

回答by Matthew Graves

try:

尝试:

var arrayOfObjecs = values.map(function(value_set){
                                  var obj = {}; 
                                  for(i = 0; i < keys.length; i++ )
                                        obj[keys[i]]=value_set[i];      
                                  return obj;})

map is a great function for looping over arrays

map 是一个很好的循环数组的函数

回答by Bergi

You're on the right track.

你在正确的轨道上。

…
    var obj = {
    for(var j=0; j<values[i].length; j++){
        keys[j] : values[i][j];
    }
    };
…
…
    var obj = {
    for(var j=0; j<values[i].length; j++){
        keys[j] : values[i][j];
    }
    };
…

That's a syntax error. You can't put the for-loop in the middle of the object literal. Instead, create the empty object in one step and then fill it with the properties by assignment (using bracket notation) in the loop:

那是语法错误。您不能将 for 循环放在对象字面量的中间。相反,一步创建空对象,然后在循环中通过赋值(使用括号表示法)用属性填充它:

…
    var obj = {};
    for(var j=0; j<values[i].length; j++){
         obj[keys[j]] = values[i][j];
    }
…