DEV Community

loading...

Discussion on: How to Get an Object Length

Collapse
markokolombo profile image
Marko Kolombo

Deeply covered, nice!

As a workaround, what about Object.values().length ?

Collapse
samanthaming profile image
Samantha Ming Author

Yup, that works too! Now that i think of it, i should have cover those examples too 😅 next time 😜

Collapse
krumpet profile image
Ran Lottem

I think Object.values and Object.keys will have the same length, as they both consider only enumerable properties:

Docs for Object.values

Collapse
markokolombo profile image
Marko Kolombo • Edited

true story...

const object1 = {
  a: 'somestring',
  b: 42,
  c: false,
  [Symbol('ghost')]: '👻'
};

console.log(Object.values(object1).length); //>3
Thread Thread
krumpet profile image
Ran Lottem

I'm getting 3 for the length of both keys and values. The arrays are ["a", "b", "c"] and ["somestring", 42, false], respectively.