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){ 

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++){ 

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.

The object
Will be options. An object.

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

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: