0

hi guys I have an object like this

const tmp ={
    identityType:null,
    identityNumber:null,
    nationality:null,
    firstName:null,
    secondName:null,
    gender:null,
    birthDate:null,
    bornCountry:null,
    profession:null,
    province:null,
    city:null,
    address:null,
    postCode:null,
    img:null,
  };

can I check the tmp if all data is not null I will do something ?, I know the easy way just use if , but the data is too much if I'm using If operator ?

2

2 Answers 2

3

Use the every() method to test if all elements of an array fit some criteria. And Object.values() to get the values in the object as an array.

if (Object.values(tmp).every(el => el !== null)) {
    console.log("All values are not null");
}
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to check if all values are nulls just check if

Object.values(tmp).filter(el => el !== null).length === 0

The same for all falsey values (null, undefined, 0, etc.)

Object.values(tmp).filter(el => Boolean(el)).length === 0

2 Comments

every is better than filter for this. It doesn't create a new array, and stops as soon as it knows the answer.
@Barmar, yes indeed, you're right, I simply forgot about every. Post your answer and I'll upvote it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.