title: Update nested object property based on the value of another property - JS
body: <p>I have a nested object and I would like to traverse it and update the property of 'error' to true if the 'value' property is an empty/falsy value. If the value of 'value' property is empty string, empty array, empty object, undefined or null, the corresponding 'error' property should be changed to true.</p>
  <p>This is what I have:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const data = {
    "item1": {
      "value": 88,
      "error": false
    },
    "item2": {
      "value": 655,
      "error": false
    },
    "item3": false,
    "item4": [],
    "item5": "",
    "item6": "",
    "item7": false,
    "item8": {
      "value": undefined,
      "error": false
    },
    "item9": {
      "value": [],
      "error": false
    },
    "item10": {
      "value": [],
      "error": false
    },
    "item11": {
      "value": [],
      "error": false
    },
    "item12": false,
    "item13": {
      "subItem1": {
        "name": "Country",
        "group": {
          "value": {},
          "error": false
        },
        "instances": []
      },
      "subItem2": {
        "name": "Group",
        "group": {
          "value": {},
          "error": false
        },
        "instances": []
      },
      "subItem3": {
        "name": "Product",
        "group": {
          "value": {},
          "error": false
        },
        "instances": []
      }
    }
  }
  
  function iter(o) {
    Object.keys(o).forEach(function(k) {
      if (o[k] !== null &amp;&amp; typeof o[k] === 'object') {
        iter(o[k]);
        return;
      }
      if (o['value'] === undefined || !o['value'] || _.isEmpty(o.value)) {
        o['error'] = true
      }
    });
  }
  
  
  iter(data)
  
  console.log(data)</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>expected output:</p>
  <pre><code>{
    &quot;item1&quot;: {
      &quot;value&quot;: 88,
      &quot;error&quot;: false
    },
    &quot;item2&quot;: {
      &quot;value&quot;: 655,
      &quot;error&quot;: false
    },
    &quot;item3&quot;: false,
    &quot;item4&quot;: [],
    &quot;item5&quot;: &quot;&quot;,
    &quot;item6&quot;: &quot;&quot;,
    &quot;item7&quot;: false,
    &quot;item8&quot;: {
      &quot;value&quot;: undefined,
      &quot;error&quot;: true
    },
    &quot;item9&quot;: {
      &quot;value&quot;: [],
      &quot;error&quot;: true
    },
    &quot;item10&quot;: {
      &quot;value&quot;: [],
      &quot;error&quot;: true
    },
    &quot;item11&quot;: {
      &quot;value&quot;: [],
      &quot;error&quot;: true
    },
    &quot;item12&quot;: false,
    &quot;item13&quot;: {
      &quot;subItem1&quot;: {
        &quot;name&quot;: &quot;Country&quot;,
        &quot;group&quot;: {
          &quot;value&quot;: {},
          &quot;error&quot;: true
        },
        &quot;instances&quot;: []
      },
      &quot;subItem2&quot;: {
        &quot;name&quot;: &quot;Group&quot;,
        &quot;group&quot;: {
          &quot;value&quot;: {},
          &quot;error&quot;: true
        },
        &quot;instances&quot;: []
      },
      &quot;subItem3&quot;: {
        &quot;name&quot;: &quot;Product&quot;,
        &quot;group&quot;: {
          &quot;value&quot;: {},
          &quot;error&quot;: true
        },
        &quot;instances&quot;: []
      }
    }
  }
  </code></pre>
  <p>Please advice.</p>