arrowill12
arrowill12

Reputation: 1804

jquery each not iterating over object

I am currently doing the following. I am lost as to why this simple function is not working. I am building an object dynamically and then I want to iterate over it. I have checked and the object does have content in it.

newChecked =[];

$.each(checked, function(index, value) {
newChecked[value.app]=value.admin;
});

$.each(newChecked, function(index, value) {
    //do some stuff here while iterating
});

I have put debugging lines inside the second .each and they are not being returned. it is completely ignoring the second .each

Upvotes: 0

Views: 441

Answers (2)

plalx
plalx

Reputation: 43718

I think that you are misunderstanding the use of an array. If you want an associative array/map, use a plain object instead of an array.

var newChecked = {};

$.each(checked, function(index, value) {
    newChecked[value.app] = value.admin;
});

$.each(newChecked, function (k, v) {
    console.log(v);
});

Upvotes: 1

Rahul Meghlan
Rahul Meghlan

Reputation: 11

you have defined newChecked an array, thus please ensure that value.app is of type number

eg: if checked = checked = [{app : 0, "admin" : "cba"}, {app : 1, "admin" : "fed"}]

than newChecked will be : ["cba", "fed"]

Whereas if checked = [{app : 0, "admin" : "cba"}, {app : 2, "admin" : "fed"}],

then undefined will be added to the index 2 of array newChecked :

["cba", undefined, "fed"]

Upvotes: 0

Related Questions