Boky
Boky

Reputation: 12064

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?

Upvotes: 1

Views: 73

Answers (2)

ninjawarrior
ninjawarrior

Reputation: 326

You can use the every function of loadash to check if active is false for every object.

var data = [
    {type: "Benzine", active: false},
    {type: "Diesel", active: false},
    {type: "Electricity", active: true}
];
// First argument is the data and second argument is the predicate to check
var res = _.every(data, {active: false}); // Returns true if all elements pass the predicate match else false.
document.getElementById("data").innerHTML  =  res;

Upvotes: 1

woutr_be
woutr_be

Reputation: 9722

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));

Upvotes: 2

Related Questions