The body of a for-in should be wrapped in an if statement to filter unwanted properties from the pro

In ESLint mode, for in will report an error when traversing the object, which can be solved as follows:

let  val  = { shu: [ 1 , 2 , 3 ]} ;
for  (let  item  in  val) {
  if  (val.hasOwnProperty(item)) { 
    console.log(item);
  }
}

Because we should judge whether the object has its own properties (non-inheritance) before traversing an object. If the object is an empty object, then there is no need to traverse it to improve the efficiency of the code.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *