0

I have below object inside array

    [
    {
        "age":32,
        "test":true
    },
    {
        "age":33,
        "test":true
    },
    {
        "age":35,
        "test":false
    }
]

I need to check if all values of test is false.

I have tried below code

Array.isArray(obj.map((message,index) => {
      if(message.test !== message.test){
          //trigger when all values are false
      }
}))

How to achieve this?

2 Answers 2

7

You can use every from Array prototype:

let areAllFalse = array.every(x => x.test === false);
Sign up to request clarification or add additional context in comments.

2 Comments

Can be shortened to array.every(x => !x.test)
@bugs possibly can be but he explicitly mentioned to check against false not undefined and other falsy values
0

You can also you filter from array prototype...

const filtered = array.filter(a => a.test === true)

or the less verbose

const filtered = array.filter(a => a.test)

2 Comments

if (filtered) { ... }
So you just told me how you would tell if they were all false, you might want to add that to the answer as OP seems new and may not know that. Because right now you are showing how to filter an array without answering the question and helping OP learn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.