Boky Boky - 10 months ago 63
React JSX Question

Check if all properties are false

React state is as follows :

[
{type: "Benzine", active: false},
{type: "Diesel", active: false},
{type: "Electricity", active: false}
]


How can I check if all
active
values are false.

Is there any way to do it with lodash?

Answer Source

You can use the following to test wether every active property is true:

    var arr = [
       {type: "Benzine", active: false},
       {type: "Diesel", active: false}, 
       {type: "Electricity", active: false}
    ]

    console.log(arr.every(obj => obj.active));

var arr = [
       {type: "Benzine", active: true},
       {type: "Diesel", active: true}, 
       {type: "Electricity", active: true}
   ]

   console.log(arr.every(obj => obj.active));

    var arr = [
       {type: "Benzine", active: false},
       {type: "Diesel", active: true}, 
       {type: "Electricity", active: false}
    ]

    console.log(arr.every(obj => obj.active));