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

标签 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 - 处理器英特尔至强 E5-2630 |第 1 列:0.64 |第 2 列:0.7 |第 3 列:0.665

注意:Column 1、Column 2、Column 3的名称是动态的,在创建这个jSon对象时可以是任意名称。

附加信息:

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

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

最佳答案

我会使用 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])
        }
    }
}

关于javascript - 如何循环嵌套的 Json 访问所有对象的相同元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54766408/

相关文章:

loops - 嵌套循环的复杂性

javascript - 登录按钮重定向到两个页面。对于首次登录者,它会重定向到协议(protocol)页面,对于休息,它会直接重定向到主页?

javascript - Google数据可视化使用两个数据集堆叠条形图

javascript - 足够安全,可以使用 javascript 进行加密和解密

python - 从 RDF 查询结果打印/解析 JSON 对象

database - Perl 在 HTML::Template 中嵌套了 TMPL_LOOP

javascript - JavaScript 中未定义与已定义的窗口属性

java - Jackson:不同的 XML 和 JSON 格式

python - 使用 Python 和 Pandas 访问 JSON 元素

java - 嵌套循环不会在长度大于 1 的内部循环中执行查询