Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Empty Object?
Manav Misra
Manav Misra

Posted on

     

Empty Object?

At times, for example, when receiving an object as afunction parameter, we wish to know if we have received an empty object or not.

This can especially be the case if some JSON data in arequest body is not parsed correctly; our server then ends up with an empty object.

functioncheck4ValidObjectWithKeys(someObj){if(Object.entries(someObj).length){return"👍🏾"}return"👎🏾"}
Enter fullscreen modeExit fullscreen mode

Object.entries...

...along with things likeObject.keys andObject.values (all would work for the example) creates an array of either...

  1. The entries - 🔑/value pairs (it's an array of arrays!)
  2. Just the 🔑s - again, an array
  3. Just the values - what is it?...an array!

.length...

...gives us the length (number of items) in an array as anumber.

Coercion withif

if (Object.entries(someObj).length) { translates to:

  1. Get the entries from the object as an array
  2. If thelength of that array is considered as 'truthy' (non-zero)...

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a JS Subject Matter Expert (SME) that has spent the past few years spearheading curricula and teaching initiatives at colleges and bootcamps, in person and virtually.
  • Location
    62236
  • Education
    BS - Mech. Eng. - Missouri S&T
  • Joined

More fromManav Misra

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp