title: Remove empty & null values from nested object (ES6) - Clean nested Objects
body: <p>I got an object which looks like this :</p>
  
  <pre><code>{
      "a": "string not empty",
      "b": {
          "c": "string not empty",       
      },
      "d": {
          "e": false,
          "f": 0,
          "g": true,
          "h": 10
      },
      "i": {
          "j": 0,
          "k": null
      },
      "l": {
          "m": null
      },
      "n": {
          "o": 1,
          "p": "string (not empty)",
          "q": {}
      },
      "r": [],
      "l": "2000-01-01T01:01:00.000Z",
  }
  </code></pre>
  
  <p>Thanks to the code provided by here : <a href="https://stackoverflow.com/a/38364486/3912805">https://stackoverflow.com/a/38364486/3912805</a> I can now remove all <code>null</code> values of my nested object.</p>
  
  <p>I used this function so far to <code>removeNull</code> :</p>
  
  <pre><code>removeNull = (obj) =&gt; {
    Object.keys(obj).forEach(key =&gt;
      (obj[key] &amp;&amp; typeof obj[key] === 'object') &amp;&amp; removeNull(obj[key]) ||
      (obj[key] === undefined || obj[key] === null) &amp;&amp; delete obj[key]
    );
    return obj;
  };
  </code></pre>
  
  <p>But I would like to enhance this function to allow me to remove all empty arrays  or any empty collection which may exists in my nested object.</p>
  
  <p>Final results should be <em>without</em> <code>k</code>, <code>l</code> &amp; <code>m</code>, <code>q</code>, <code>r</code>, <code>l</code>:</p>
  
  <pre><code>{
      "a": "string not empty",
      "b": {
          "c": "string not empty",       
      },
      "d": {
          "e": false,
          "f": 0,
          "g": true,
          "h": 10
      },
      "i": {
          "j": 0
      },
      "n": {
          "o": 1,
          "p": "string (not empty)"
      },
      "l": "2000-01-01T01:01:00.000Z",
  }
  </code></pre>
  
  <p>I need to keep all values which were set to <code>0</code> or to <code>false</code>.</p>
  
  <p>I would like to enhance this <code>removeNull</code>'s method using ES6 method, but so far I failed to do it.</p>
  
  <p>I also tried old school method which was used for this <a href="https://stackoverflow.com/questions/52175209/how-to-deeply-remove-null-values-empty-objects-and-empty-array-from-an-object">How to deeply remove null values, empty objects and empty array from an object</a></p>
  
  <pre><code>itemToBool = item =&gt; {
    if (typeof item !== 'object' || item === null) return item;
    const cleanedItem = cleanObject(item);
    return Object.keys(cleanedItem).length !== 0 &amp;&amp; cleanedItem;
  };
  
  cleanObject = obj =&gt; {
    if (Array.isArray(obj)) {
      const newArr = obj.map(itemToBool).filter(Boolean);
      return newArr.length &amp;&amp; newArr;
    }
    const newObj = Object.entries(obj).reduce((a, [key, val]) =&gt; {
      const newVal = itemToBool(val);
      if (newVal !== null || newVal === false) a[key] = newVal;
      return a;
    }, {});
    return Object.keys(newObj).length &gt; 0 &amp;&amp; newObj;
  };
  </code></pre>
  
  <p>but it fails too.</p>