将数组合并到嵌套对象中

时间:2014-01-14 18:13:58

标签: javascript oop

所以这是一个小小的冒险,但这里有。

假设我有两个数组 - 一个包含一组类别的一维数组,以及一个包含任意数量数组的二维数组,这些数组的内容遵循第一个数组建立的类别模板。例如:

var categoryArray = ["Name", "Title", "Hire Date"];
var infoArray = [["John","Project Manager","January 5"], ["Alex","Accountant","December 15"], ["Joanne","Graphic Designer","August 26"]];

我想要做的是将这些信息整合到一个对象中。最重要的是我放弃了:

var myDict = {};

我(显然是错误的)尝试是嵌套for循环,它遍历每个数组并尝试用数组的内容填充myDict对象。它看起来像这样:

// Start by iterating by the length of the info array, so we assign a new sub-object to the myDict Object for each entry.
for (i = 0; i < infoArray.length; i++) {
    // Each entry will be a new "row," like in a spreadsheet. 
    var row = String("row"+i);
    // I'm guessing that the declaration below doesn't actually assign the "Row1", "Row2", etc as nested objects like I had intended, but just re-writes a child called "row"
    myDict.row = {};
    // Next we iterate through the number of categories we'll need-- and we'll pull from the length of our categoryArray so we can change the number of categories later.
    for (x = 0; x < categoryArray.length; x++) {
        // In theory, the first iteration of this will create a child of "row1" called "name," which will hold a value of "John" (so, the value stored infoArray[1][1])
        myDict.row.categoryArray[x] = infoArray[i][x];
    }
}

那里显然有很多错误,但我完全不知道如何解决这个问题。我想关键问题是尝试将变量/数组所拥有的实际字符串分配为对象/子项的名称,但是彻底的谷歌搜索没有产生任何答案。

拯救我哦强大的互联网!

3 个答案:

答案 0 :(得分:1)

var result = infoArray.map(function(currentArray) {
    return categoryArray.reduce(function(previous, currentKey, index) {
        previous[currentKey] = currentArray[index];
        return previous;
    }, {});
});

<强>输出

[ { Name: 'John',
    Title: 'Project Manager',
    'Hire Date': 'January 5' },
  { Name: 'Alex',
    Title: 'Accountant',
    'Hire Date': 'December 15' },
  { Name: 'Joanne',
    Title: 'Graphic Designer',
    'Hire Date': 'August 26' } ]

如果您的环境不支持Array.prototype.map功能,则可以使用此

var result = [];
for (var i = 0; i < infoArray.length; i += 1) {
    var tempObj = {};
    for (var j = 0; j < categoryArray.length; j += 1) {
        tempObj[categoryArray[j]] = infoArray[i][j];
    }
    result.push(tempObj);
} 

答案 1 :(得分:0)

// I'm guessing that the declaration below doesn't actually assign the "Row1", "Row2", etc as nested objects like I had intended, but just re-writes a child called "row"
myDict.row = {};

完全。为此,您必须使用

 myDict[row] = {};

(以下同样:myDict[row][…] = …

// In theory, the first iteration of this will create a child of "row1" called "name," which will hold a value of "John" (so, the value stored infoArray[1][1])
myDict.row.categoryArray[x] = infoArray[i][x];

此处.categoryArray[x]再次用作文字属性名称(属性x的属性"categoryArray",但不存在) - 您必须wrap it in brackets

你最终应该:

var myDict = {};
for (var i = 0; i < infoArray.length; i++) { // make iteration variables local
                                             // with "var"
    var row = "row"+i; // explicit String() call not necessary
    myDict[row] = {};
    for (var x = 0; x < categoryArray.length; x++) { // again, local "x"
        myDict[row][categoryArray[x]] = infoArray[i][x];
    }
}

答案 2 :(得分:0)

使用row0row1等并不是一个好主意。数组更有意义。无论如何:

var categoryArray = ["Name", "Title", "Hire Date"];
var infoArray = [["John","Project Manager","January 5"],["Alex","Accountant","December 15"],["Joanne","Graphic Designer","August 26"]];
var myDict = {};

for (i = 0; i < infoArray.length; i++) {
    // Each entry will be a new "row," like in a spreadsheet. 
    var row = {};
    myDict["row"+i] = row;
    // Next we iterate through the number of categories we'll need-- and we'll pull from the length of our categoryArray so we can change the number of categories later.
    for (x = 0; x < categoryArray.length; x++) {
        // In theory, the first iteration of this will create a child of "row1" called "name," which will hold a value of "John" (so, the value stored infoArray[1][1])
        row[categoryArray[x]] = infoArray[i][x];
    }
}

<强>输出:

{
    "row0": {
        "Name": "John",
        "Title": "Project Manager",
        "Hire Date": "January 5"
    },
    "row1": {
        "Name": "Alex",
        "Title": "Accountant",
        "Hire Date": "December 15"
    },
    "row2": {
        "Name": "Joanne",
        "Title": "Graphic Designer",
        "Hire Date": "August 26"
    }
}