Search code examples
javascriptjsonnested-loops

How to loop nested Json accessing same th element for all objects?


I have a Json object that has 4 children: 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}
    }

I need to capture the column name (object name) and access the th element for all the objects in orchestrate mode. For example:

Item: PD | Column 1: 0.2 | Column 2: 0.24 | Column 3: 0.199

Item: 1 - Processor Intel Xeon E5-2630 | Column 1: 0.64 | Column 2: 0.7 | Column 3: 0.665

Note: the name of Column 1, Column 2 and Column 3 are dynamic, can be any name when creating this jSon object.

Additional Info:

I am using Javascript and I tried doing a loop only in the first object and getting the same position cell for the other objects, but as the other objects' names are dynamic, I don't know how to do it.

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++;}}

Solution

  • I would access the properties dynamically using Object.keys. That way you can iterate over your first property and map the values from the other properties to it. Say, e.G. 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])
            }
        }
    }