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}]
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);
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
You could Loop over the array and check every property.
var array = [
{"id":100,"output":false},
{"id":100,"output":false},
{"id":100,"output":true}
];
function testOutput ( array ) {
for (el in array)
if ( el.output ) return true;
return false;
}
testOutput (array);
Best Regards
fastest + most compatible
var result = false var json = [{"id":100,"output":false},{"id":100,"output":false},{"id":100,"output":true}] for( var i = json.length; !result && i; result = json[--i].output ); console.log("at least one? ", result)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.