4

My question is in regards to the following function:

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

The 'configs' object that the loadConfigurations function receives, contains two properties --two arrays one called "assigned" the other called "unassigned". And executing console.log(configs) outputs the following:

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

Now I want to enumerate the key-value pairs of each property in both the 'assigned' array and the 'unassigned array'.

The configs object is structured in the following way:

Sorry fellas. This is the structure of the configs objects:

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"
            }
            ]
        }

How do I do this?

Thanks, CM

Chris Mazzochi
  • 179
  • 1
  • 15
  • 1
    Firstly note that you have two arrays, not key/value pairs. To achieve what you need you can use two loops, one over `configs.assigned` and another over `configs.unassigned`. – Rory McCrossan Sep 30 '16 at 15:30
  • Arrays don't really have key-value pairs, just indices with content, but maybe `configs.assigned.concat(configs.unassigned).forEach( (index, value) => ...` – adeneo Sep 30 '16 at 15:31
  • `In each array there are key value pairs I want to get at.` This cannot be the case, arrays cannot contain key/values. It would help if you could post the complete contents of the object instead of the console representation of it – Rory McCrossan Sep 30 '16 at 15:35
  • Sorry. Yeah each array contains objects that do contain key value pairs. And okay I know arrays have indexes with contents. – Chris Mazzochi Sep 30 '16 at 15:38
  • I added a working example for you which shows how to enumerate the keys of the object. – Rory McCrossan Sep 30 '16 at 15:45

3 Answers3

2

Given the data structure you've posted you can use Object.keys() to enumerate the keys of the parent object and then loop over the assigned and unassigned properties within that. Try this:

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

Also note that (as pointed out by @adeneo in the comments on your question) if you're performing the same logic on both assigned and unassigned items then you can concatenate them together and iterate them once.

The above method would be required if the logic is different between properties.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Now how do take the name and value of each of the assigned and unassigned, and populate a select element with options containing the results something like: $('#shutle-dst').html(''); – Chris Mazzochi Sep 30 '16 at 15:49
  • That's an entirely separate goal which you should start a new question for. Although a similar question has already been answered here: http://stackoverflow.com/questions/170986/what-is-the-best-way-to-add-options-to-a-select-from-an-array-with-jquery – Rory McCrossan Sep 30 '16 at 15:50
  • This is definitely a tough programming technique isn't it. Newbie or otherwise right... – Chris Mazzochi Sep 30 '16 at 15:52
1

Try this one:

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

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

Check here Javascript unassigned var?

Community
  • 1
  • 1
vvamondes
  • 362
  • 2
  • 7
0

Iterate the objects keys, then that objects keys, then those keys arrays:

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);
        }
    }
}
tymeJV
  • 103,943
  • 14
  • 161
  • 157