Search code examples
javascript

How to perform a Boolean test on a nested javascript object?


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.


Solution

  • 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.

    EDIT: updated the code with reduce and Object.values as suggested. I'm not a big fan of reduce as I find it less readable, and it makes people think that you save a variable reference where it's the contrary under the hood, the initial value is carried from one callback to another with 3 other parameters that have to be handled internally. I completely agree with Object.values and improper use of map (the original code should have used forEach instead probably, or even a simple for)

    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){
      return Object.values(obj).reduce(function (acc, cur) {
        if(typeof cur == 'object'){
          acc = acc.concat( recurseFlat(cur) );
        }else{
          acc.push(cur);
        }
    
        return acc;
      }, []);
    }
    
    let flat = recurseFlat(data);
    
    console.log('flatten: ', flat);
    
    let result = flat.every((el) => el === true);
    
    console.log('result: ', result);

    (ORIGINAL CODE:)

    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);