What initially appeared to be a no-brainer got me scratching my head and wondering why I can't seem to iterate through a simple list of value/key pairs to reset all values to zero. My code (javascript) looks as follows:
var theme = {
facilityNRCount: 5,
facilityMOCount: 2,
facilitySMCount: 8,
....
physicalSecCount: 9,
energyWaterCount: 1,
otherSustCount: 10
};
$.each(theme, function(k, v) {
v = 0;
});
And apparently I'm missing something, which isn't so obvious to me. Here's the fiddle .
The $.each
function doesn't use references. The v
is just the value, not a reference to it.
Try this:
for(var k in theme){
if(theme.hasOwnProperty(k)){
theme[k] = 0;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.