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

Checking if any values in object are not null

I have the following code:

err = {
  "content-type": null,
  phone: null,
  firstName: null,
  lastName: null,
  email: null,
  website: null,
  supplier: null
}

console.log(Object.keys(err).some(key => key !== null))

I learnt about the .some function from stackoverflow, but this is returning true. All the values are null, so I would expect it to return false.

I just need a way to check if any of the values are not null, then I know there are errors in the form.

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 :

Use the Object.values() method to get an array of the object’s values.
Use the Array.every() method to iterate over the array.
Check if each value is equal to null.
The every() method will return true if all values are null.

const err = {
  "content-type": null,
  phone: null,
  firstName: null,
  lastName: null,
  email: null,
  website: null,
  supplier: null
}

const result = Object.values(err).every(value => {
  if (value === null) {
    return true;
  }

  return false;
});

console.log(result);

Reference

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