Tag Archives: Object.keys

for..in loops iterate over the entire prototype chain, which is virtually never what you want.

for..in loops iterate over the entire prototype chain, which is virtually never what you want.

It means that using for..in will traverse the entire prototype chain, which is not a good way to implement it. Object.keys is recommended

formRules : {
  name : true,
  cardType: true,
  certificateNo: true
 },
 formData : {
  name :  '' ,
  certificateType:  '' ,
  certificateNo:  ''
 }

Original code

for ( const key in  this .formData) {
     if (! this .formData[key]) { this .formRules[key] = false ; valid = false }
     if ( this .formData[key]) this .formRules[key] = true
}

Modified code

Object.keys( this .formData).forEach(key => {
 if (! this .formData[key]) { this .formRules[key] = false ; valid = false }
 if ( this .formData[key]) this .formRules[key] = true
})