Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find the length of object values which is not empty

I have an object, for example ,

let obj = {
  name: '',
  age: 16, 
  address: '', 
  city: '', 
  pin: 4564
}

I want the length of element which have a value rather than 0 or undefined or ”.
my obj has 2 elements which have value( age and pin) soI want the length of obj as 2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

using Object.values and filter with Boolean on each element on it maybe

let obj = {
  name: '',
  age: 16, 
  address: '', 
  city: '', 
  pin: 4564
}

console.log(Object.values(obj).filter(Boolean).length)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading