Matarishvan
Matarishvan

Reputation: 2422

Check all object values inside Array javascript has boolean false

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?

Upvotes: 0

Views: 104

Answers (2)

Steven Mays
Steven Mays

Reputation: 365

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)

Upvotes: 0

mickl
mickl

Reputation: 49945

You can use every from Array prototype:

let areAllFalse = array.every(x => x.test === false);

Upvotes: 7

Related Questions