skip to Main Content

I want to check if all value in the object are true, i want to return true if all are true but return false if any one is false

{
    "2211284396232-5": {
        "2211284396232-5": true, 
        "644889430161": {
            "4812219518053": false, 
            "4812219518055": false,
            "4812219518063": false,
            "4812219518064": true,
            "4812219518069": false, 
            "644889430161": false
        }, 
    },
    
    "2211628865398-6": {
        "2211628865398-6": false, 
            "729563966440": {
                "5229922707373": false, 
                "729563966440": false
            }, 
    },
    
     "2212485574058-4": {
        "2212485574058-4": false, 
            "689399023594": {
                "5077186698704": true, 
                "5077186698712": false, 
                "689399023594": false
            }, 
    },
     
 }

I know of every() method but my problem is the object are nested so i don’t know how to go about it. any help would be appreciated.

2

Answers


  1. Write recursive tester. Loop all values and check if it’s boolean. If boolean – compare to true and return. If object – call itself once more.

    let data1 = {a: true, b: {c: true, d: {e: true}}};
    let data2 = {a: true, b: {c: true, d: {e: false}}};
     
     function test(data, comparison) {
         if (typeof data == 'boolean') {
             return data === comparison;
         }
     
         for (let i in data) {
             if (!test(data[i], comparison)) {
                 return false;
             }
         }
         
         return true;
     }
     
     console.log(test(data1, true), test(data2, true));
    Login or Signup to reply.
  2. What you can do is recursively flatten the object to an array of the values. Then you can call every on the result to test your result.

    let data = {
        "2211284396232-5": {
            "2211284396232-5": true, 
            "644889430161": {
                "4812219518053": false, 
                "4812219518055": false,
                "4812219518063": false,
                "4812219518064": true,
                "4812219518069": false, 
                "644889430161": false
            }, 
        },
        
        "2211628865398-6": {
            "2211628865398-6": false, 
            "729563966440": {
                "5229922707373": false, 
                "729563966440": false
            }, 
        },
        
         "2212485574058-4": {
            "2212485574058-4": false, 
            "689399023594": {
                "5077186698704": true, 
                "5077186698712": false, 
                "689399023594": false
            }, 
        },
         
     };
     
    function recurseFlat(obj){
      let acc = [];
      
      Object.keys(obj).map(function (k) {
        if(typeof obj[k] == 'object'){
          acc = acc.concat( recurseFlat(obj[k]) );
        }else{
          acc.push(obj[k]);
        }
      });
        
      return acc;
    }
    
    let flat = recurseFlat(data);
    
    console.log('flatten: ', flat);
    
    let result = flat.every((el) => el === true);
    
    console.log('result: ', result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search