Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check javascript array of objects property

I have the following javascript array of objects ,I need to check output property if at least one object is true return true else return false,Can anyone help me to implement that?

var array=[{"id":100,"output":false},{"id":100,"output":false},
{"id":100,"output":true}]    
like image 742
Ali-Alrabi Avatar asked Oct 18 '25 15:10

Ali-Alrabi


2 Answers

You could use Array#some

The some() method tests whether some element in the array passes the test implemented by the provided function.

var array = [{ "id": 100, "output": false }, { "id": 100, "output": false }, { "id": 100, "output": true }];
    result = array.some(function (a) { return a.output; });

console.log(result);
like image 182
Nina Scholz Avatar answered Oct 20 '25 05:10

Nina Scholz


function hasOneTrue(a){
  return !!a.filter(function(v){
    return v.output;
  }).length;
}

var array = [{"id":100,"output":false}, {"id":100,"output":false}, {"id":100,"output":true}]
console.log(hasOneTrue(array)); // true
like image 34
Robiseb Avatar answered Oct 20 '25 05:10

Robiseb