DEV Community

Discussion on: How to Get an Object Length

Collapse
 
samanthaming profile image
Samantha Ming

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.