Usage example resolution of for in in js

  • 2020-03-30 01:04:17
  • OfStack

For (var I = 0; I< Len. I++) is generally used instead of for in.
Such as:
 
var a = ["a","b","c"]; 
for(var el in a){ 
alert(a[el]); 
} 

So this is going to be all of the elements in a, and of course this example can be used
 
for(var i=0,len=a.length;i<len;i++){ 
alert(a[i]); 
} 

This is a way of looping the list, but sometimes it doesn't work.
Such as:
 
var a = {"first":1,"second":2,"third":3}; 

For in, for in, for in, for in, for in

Whether an object can be used for in exhaustion can be determined by using the propertyIsEnumerable property, as follows:
PropertyIsEnumerable properties
Returns a Boolean value indicating whether the specified property is part of an object and whether the property is enumerable.
 
object.propertyIsEnumerable(proName) 

parameter
The object
Will be options. An object.

proName
Will be options. The string value of a property name.

instructions
If proName exists in object and you can use a For... If the In loop is exhausted, the propertyIsEnumerable property returns true. The propertyIsEnumerable property returns false if the object does not have the specified property or if the specified property is not enumerable. Typically, predefined properties are not enumerable, while user-defined properties are always enumerable.
The propertyIsEnumerable property does not consider objects in the prototype chain.

Related articles: