Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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 :-)

like image 832
dj_boy Avatar asked Nov 24 '25 08:11

dj_boy


2 Answers

Or a bit more functionally:

Object.keys(success).every(function(key) {
  return success[key];
});
like image 101
sabof Avatar answered Nov 25 '25 22:11

sabof


var everythingOK = true;

for (var i = 0; i < success.length; i++)
{
    if( ! success[i])
    {
         everythingOK = false;
         break;
    }
} 


if(everythingOK) 
    alert('Success!');

this should do ;)

like image 29
Kevin Cittadini Avatar answered Nov 25 '25 22:11

Kevin Cittadini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!