迭代并枚举对象中多个数组的键值对

时间:2016-09-30 15:29:17

标签: javascript jquery arrays javascript-objects

我的问题是关于以下功能:

function loadConfigurations(configs){
  console.log(configs);
}

'配置' loadConfigurations函数接收的对象包含两个属性 - 一个名为"被分配的"另一个名为"未分配"。并执行console.log(configs)输出以下内容:

"Object{assigned: Array[5], unassigned: Array[1]}"

现在,我想要枚举“已分配的”'中每个属性的键值对。数组和未分配的数组'。

configs对象的结构如下:

抱歉fellas。这是configs对象的结构:

var configs = {
"config1":
        {
        "assigned": [
            {
                "name": "Admin Usersss",
                "value": "admin-user"
            },
            {
                "name": "MPR User",
                "value": "mpr-user"
            },
            {
                "name": "SAMHSA User",
                "value": "samhsa-user"
            }
         ],
        "unassigned": [
            {
                "name": "States User",
                "value": "states-user"
            },
            {
                "name": "All States User",
                "value": "all-states-user"
            },
            {
                "name": "Substance Abuse User",
                "value": "substance-abuse-user"
            }
         ]
        },
"config2":
        {
        "assigned": [
            {
                "name": "Admin User",
                "value": "admin-user"
            },
            {
                "name": "MPR User",
                "value": "mpr-user"
            },
            {
                "name": "SAMHSA User",
                "value": "samhsa-user"
            },
            {
                "name": "States User",
                "value": "states-user"
            },
            {
                "name": "All States User",
                "value": "all-states-user"
            }
        ],
        "unassigned": [
            {
                "name": "Substance Abuse User",
                "value": "substance-abuse-user"
            }
        ]
    },
    "config3":
    {
        "assigned": [
            {
                "name": "Admin User",
                "value": "admin-user"
            }
        ],
        "unassigned": [
            {
                "name": "States User",
                "value": "states-user"
            },
            {
                "name": "All States User",
                "value": "all-states-user"
            },
            {
                "name": "Substance Abuse User",
                "value": "substance-abuse-user"
            },
            {
                "name": "MPR User",
                "value": "mpr-user"
            },
            {
                "name": "SAMHSA User",
                "value": "samhsa-user"
            }
            ]
        }

我该怎么做?

谢谢, CM

3 个答案:

答案 0 :(得分:2)

根据您发布的数据结构,您可以使用Object.keys()枚举父对象的键,然后遍历其中的assignedunassigned属性。试试这个:

Object.keys(configs).forEach(function(key) {
    var config = configs[key];

    config.assigned.forEach(function(assigned) {
        console.log('A: ' + assigned.name + ', ' + assigned.value);
    });

    config.unassigned.forEach(function(assigned) {
        console.log('U: ' + assigned.name + ', ' + assigned.value);
    });
})



var configs = {
  "config1": {
    "assigned": [{
      "name": "Admin Usersss",
      "value": "admin-user"
    }, {
      "name": "MPR User",
      "value": "mpr-user"
    }, {
      "name": "SAMHSA User",
      "value": "samhsa-user"
    }],
    "unassigned": [{
      "name": "States User",
      "value": "states-user"
    }, {
      "name": "All States User",
      "value": "all-states-user"
    }, {
      "name": "Substance Abuse User",
      "value": "substance-abuse-user"
    }]
  },
  "config2": {
    "assigned": [{
      "name": "Admin User",
      "value": "admin-user"
    }, {
      "name": "MPR User",
      "value": "mpr-user"
    }, {
      "name": "SAMHSA User",
      "value": "samhsa-user"
    }, {
      "name": "States User",
      "value": "states-user"
    }, {
      "name": "All States User",
      "value": "all-states-user"
    }],
    "unassigned": [{
      "name": "Substance Abuse User",
      "value": "substance-abuse-user"
    }]
  },
  "config3": {
    "assigned": [{
      "name": "Admin User",
      "value": "admin-user"
    }],
    "unassigned": [{
      "name": "States User",
      "value": "states-user"
    }, {
      "name": "All States User",
      "value": "all-states-user"
    }, {
      "name": "Substance Abuse User",
      "value": "substance-abuse-user"
    }, {
      "name": "MPR User",
      "value": "mpr-user"
    }, {
      "name": "SAMHSA User",
      "value": "samhsa-user"
    }]
  }
}

Object.keys(configs).forEach(function(key) {
  var config = configs[key];
  config.assigned.forEach(function(assigned) {
    console.log(key + ' A: ' + assigned.name + ', ' + assigned.value);
  });
  config.unassigned.forEach(function(assigned) {
    console.log(key + ' U: ' + assigned.name + ', ' + assigned.value);
  });
})




另请注意(如@adeneo在您的问题评论中所指出的)如果您在assignedunassigned项目上执行相同的逻辑,那么您可以将它们连接在一起并迭代它们一次。

如果属性之间的逻辑不同,则需要上述方法。

答案 1 :(得分:1)

试试这个:

if(configs.assigned != undefined && configs.assigned != null && configs.assigned.length > 0) {
         // LOOP
}

if(configs.unassigned != undefined && configs.unassigned != null && configs.unassigned.length > 0) {
     // LOOP
}

点击此处Javascript unassigned var?

答案 2 :(得分:0)

迭代对象键,然后是对象键,然后是那些键数组:

for (var config in configs) {
    for (var configType in configs[config]) {
        for (var i = 0; i < configs[config][configType].length; i++) {
            var props = configs[config][configType][i];
            console.log(props);
        }
    }
}