In our daily development, objects are used frequently. It is very convenient for us to calculate the length of arrays, but how to calculate the length of objects?
If we have a library project with a group of books and authors, like the following:
We analyze the current requirements. We send data to an API, but the book length cannot exceed 100, so we need to calculate how many books there are in an object before sending data. So what do we always do? We may do this:
Let's use the keys method for arrays:
The Object.keys() method will return an array of attribute names of all enumerable properties of a given object. The order of attribute names in the array is the same as that of traversing the object using the for in loop (the main difference between the two is that for in will also traverse the enumerable properties an object inherits from its prototype chain).