javascript - JS - iterate through an object and change its attribute values? -
suppose below wanted change 'valid' 'a','b' , 'c' equal true. object foo.
var foo = { a: { valid: false, required: true }, b: { valid: false, required: true }, c: { valid: false, required: true } }; (var key in foo) { var obj = foo[key]; (var prop in obj) { if (obj.hasownproperty(prop)) { //how can assign valid true here? }; }
you're making more complicated needs be.
just this:
for( var key in foo ) { foo[key].valid = true; }
or, if you're concerned code in page may have extended object.prototype
enumerable property, can instead:
for( var key in foo ) { if( foo.hasownproperty(key) ) { foo[key].valid = true; } }
but nobody should ever extend object.prototype
enumerable property. breaks kinds of code. it's unlikely issue you'd need worry about.
Comments
Post a Comment