The method for calculating the object length in javascript.
Calculates the length of an object, that is, the number of Object Attributes. The details are as follows:
Method 1:Traverse an object through for in and use hasOwnProperty to determine whether it is an object's own enumerable property.
Var obj = {"c1": 1, "c2": 2}; function countProperties (obj) {for (var property in obj) {if (Object. prototype. hasOwnProperty. call (obj, property) {count ++;})} return count;} var len = obj. length; console. log (len); // The result is 2.
Method 2:Use Object. keys () to obtain an array composed of the enumerated attributes of an Object, and use length to obtain the Object length.
Var obj = {"c1": 1, "c2": 2}; var arr = Object. keys (obj); var len = arr. length; console. log (len); // The result is 2.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.