DEV Community

loading...

Discussion on: How to Get an Object Length

Collapse
karataev profile image
Eugene Karataev

Wait.
At first you said that Object.keys don't return hidden properties.
In the end of the article you use this method to count a number of object's properties. I guess you should use Object.getOwnPropertyNames(animal).length instead of Object.keys(animal).length.

Collapse
samanthaming profile image
Samantha Ming Author

Which part? Hmm...i don't think i see it 😲

Collapse
karataev profile image
Eugene Karataev

Here's your code in the end of the article to count total amount of an object's properties.

const enumerableLength = Object.keys(animal).length;
const symbolLength = Object.getOwnPropertySymbols(animal).length;
const totalObjectLength = enumerableLength + symbolLength;
// 2 <-- 👍

Let's take a look at this object:

let animal = {head: 1};
Object.defineProperty(animal, 'legs', {value: 4});

This object has two properties, but your code above will return 1. So my thought was that it's better to replace Object.keys with Object.getOwnPropertyNames to return correct amount of an object's total properties.