[英]How to check if all keys in json equal true
how can I check if all the keys in a json object equal to true? 如何检查json对象中的所有键是否等于true? my object looks like this
我的对象看起来像这样
success = {
"first_name": false,
"middle_name": false,
"last_name": false,
"d_o_b": false,
"sex": false,
"email": false,
"re_email": false,
"password": false,
"re_password": false
};
I proccess the object and every thing that turns out ok gets changed to true, now at the end I want to check if all are true, how can I do this? 我处理了这个对象,结果确定的每一件事都变成了真的,现在最后我想检查是否都是真的,我怎么能这样做? thank's :-)
谢谢 :-)
Or a bit more functionally: 或者功能更多:
Object.keys(success).every(function(key) {
return success[key];
});
The most straightforward way is with a for
loop. 最简单的方法是使用
for
循环。 This will ensure it will work with older browsers that may not support iterators. 这将确保它可以与可能不支持迭代器的旧浏览器一起使用。
var all_true = true;
for (var s in success) {
if (!success[s]) {
all_true = false;
break;
}
}
The break
is not strictly necessary but will short-circuit the loop if all you care about if that none are false. break
并不是绝对必要的,但是如果你关心的话,如果没有错误则会使循环短路。
var everythingOK = true;
for (var i = 0; i < success.length; i++)
{
if( ! success[i])
{
everythingOK = false;
break;
}
}
if(everythingOK)
alert('Success!');
this should do ;) 这应该做;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.