简体   繁体   English

jQuery的每个不迭代对象

[英]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. 我已经在第二个.each里面放了调试行,并且它们没有被返回。 it is completely ignoring the second .each 它完全忽略了第二个.each

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

you have defined newChecked an array, thus please ensure that value.app is of type number 您已经定义了newChecked一个数组,因此请确保value.app是数字类型

eg: if checked = checked = [{app : 0, "admin" : "cba"}, {app : 1, "admin" : "fed"}] 例如:如果检查=检查= = [{app:0,“ admin”:“ cba”},{app:1,“ admin”:“ fed”}]

than newChecked will be : ["cba", "fed"] 比newChecked为:[“ cba”,“ fed”]

Whereas if checked = [{app : 0, "admin" : "cba"}, {app : 2, "admin" : "fed"}], 而如果选中= [{app:0,“ admin”:“ cba”},{app:2,“ admin”:“ fed”}],

then undefined will be added to the index 2 of array newChecked : 然后将undefined添加到数组newChecked的索引2中:

["cba", undefined, "fed"] [“ cba”,未定义,“联储”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM