admin管理员组文章数量:1355582
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}]
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}]
Share
Improve this question
asked Oct 11, 2016 at 11:43
Ali-AlrabiAli-Alrabi
1,7106 gold badges30 silver badges61 bronze badges
4
- use a for-loop :) – Ric Commented Oct 11, 2016 at 11:45
- Could you show the code and which is your issue with it? – Mario Santini Commented Oct 11, 2016 at 11:45
- Possible duplicate of How to loop through an array containing objects and access their properties – Turnip Commented Oct 11, 2016 at 11:45
- use Array.prototype.some should stop the iteration once one value is found and return a Boolean – Endless Commented Oct 11, 2016 at 11:45
4 Answers
Reset to default 11You 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 patible
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)
本文标签: Check javascript array of objects propertyStack Overflow
版权声明:本文标题:Check javascript array of objects property - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743973862a2570798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论