如何循环嵌套Json访问所有对象的相同th元素?

时间:2019-02-19 12:30:20

标签: javascript json nested-loops

我有一个Json对象,它有4个子对象:Item,Column 1,Column 2,Column 3。

    {
Item: {0: "PD", 1: "1 - Processor Intel Xeon E5-2630", 2: "2 - Additional 
    Processor", 3: "3 - Memory Quantity Increase", 4: "4 - Raid 6 H730/H730p Cabled Chassis", 5: "5 - Hard Drive Quantity Increase", 6: "6 - Perc H730 Raid Controller 1GB", 7: "7 - UEFI BIOS Boot", 8: "8 - Support: ProDeploy", 9: "9 - Hard Drive Support 3 years", 10: "10 - Dell Networking N2024P Switch", 11: "11 - N2024P Switch  - 5 Years Support", 12: "12 - N2024P Switch - Pro Deployment L2 N"},
    Column 1: {0: 0.2, 1: 0.64, 2: 0.67, 3: 0.63, 4: 0.65, 5: 0.74, 6: 0.64, 7: 0.65, 8: 0.63, 9: 0.65, 10: 1.02, 11: 0.71, 12: 0.7},
    Column 2: {0: 0.24, 1: 0.7, 2: 0.69, 3: 0.7, 4: 0.69, 5: 0.79, 6: 0.69, 7: 0.68, 8: 0.67, 9: 0.68, 10: 1.05, 11: 0.74, 12: 0.72},
    Column 3: {0: 0.199, 1: 0.665, 2: 0.652, 3: 0.631, 4: 0.644, 5: 0.698, 6: 0.647, 7: 0.637, 8: 0.622, 9: 0.63, 10: 0.997, 11: 0.698, 12: 0.672}
    }

我需要捕获列名(对象名)并以协调模式访问所有对象的th元素。例如:

项目:PD |第1列:0.2 |第2栏:0.24 |第3栏:0.199

项目:1-处理器Intel Xeon E5-2630 |第1列:0.64 |第2栏:0.7 |专栏3:0.665

注意:列1,列2和列3的名称是动态的,在创建此jSon对象时可以是任何名称。

其他信息:

我正在使用Javascript,并且尝试仅在第一个对象中进行循环,并为其他对象获取相同的位置单元,但是由于其他对象的名称是动态的,因此我不知道该怎么做。 / p>

i = 0
for (column in dfJson){         
    if(i == 0){
        for (item in column){
           console.log(dfJson['Item'][dfJson[obj][item]]); //here I have the value for the first object "Item" in this iteration
            //here I need to collect the value for Column 1 in this same position
            //here I need to collect the value for Column 2 in this same position
            //here I need to collect the value for Column 3 in this same position
}i++;}}

1 个答案:

答案 0 :(得分:2)

我将使用Object.keys动态访问属性。这样,您可以遍历第一个属性,并将其他属性的值映射到该属性。说,例如PD -> 0.2, 0.24, 0.199

const dfJson = {
	Item: {0: "PD", 1: "1 - Processor Intel Xeon E5-2630", 2: "2 - Additional Processor", 3: "3 - Memory Quantity Increase", 4: "4 - Raid 6 H730/H730p Cabled Chassis", 5: "5 - Hard Drive Quantity Increase", 6: "6 - Perc H730 Raid Controller 1GB", 7: "7 - UEFI BIOS Boot", 8: "8 - Support: ProDeploy", 9: "9 - Hard Drive Support 3 years", 10: "10 - Dell Networking N2024P Switch", 11: "11 - N2024P Switch  - 5 Years Support", 12: "12 - N2024P Switch - Pro Deployment L2 N"},
    "Column 1": {0: 0.2, 1: 0.64, 2: 0.67, 3: 0.63, 4: 0.65, 5: 0.74, 6: 0.64, 7: 0.65, 8: 0.63, 9: 0.65, 10: 1.02, 11: 0.71, 12: 0.7},
    "Column 2": {0: 0.24, 1: 0.7, 2: 0.69, 3: 0.7, 4: 0.69, 5: 0.79, 6: 0.69, 7: 0.68, 8: 0.67, 9: 0.68, 10: 1.05, 11: 0.74, 12: 0.72},
    "Column 3": {0: 0.199, 1: 0.665, 2: 0.652, 3: 0.631, 4: 0.644, 5: 0.698, 6: 0.647, 7: 0.637, 8: 0.622, 9: 0.63, 10: 0.997, 11: 0.698, 12: 0.672}
};

for (column in dfJson.Item){
    const currentKey = Object.keys(dfJson.Item)[column];
    // dfJson.item[column] -> Value of Item at current column
    console.log(dfJson.Item[column]);
    for (row in dfJson) {
        if (row !== 'Item') {
           // dfJson[row][currentKey] -> Value of dynamic Key that is not Item at current column
           console.log(dfJson[row][currentKey])
        }
    }
}