Chris Mazzochi Chris Mazzochi - 2 months ago 7
jQuery Question

Iterate and Enumerate Key Value Pairs of Multiple Arrays in an Object

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

Answer
for(var i=0; i < configs.assigned.length; i++)
{
    var item = configs.assigned[i];
    //To with item what you wanna do
}

Do the same with configs.unassigned. Btw. if you're just learning the basics of JS I would recommend reading http://www.w3schools.com/js/default.asp

Comments