0

I have a object and I want to check if myObj contains any value within any of its attribute. However this function always return true and could not figure out what wrong have I done?

function isResponsiblePersonEmpty() {
    const copyResponsiblePerson ={
        "resonsiblePersonOne": [
            {
                "uuid": "8120BC66-4BC9-4886-820E-8A9FE488F8F7",
                "name": "test",
                "company": "test",
                "availabilityGSMR": "test",
                "availabilityMobile": "test",
                "availabilityLandline": "",
                "availabilityPhoneInternal": "",
                "availabilityPhoneFallback": ""
            },
            {
                "uuid": "BFE3B5AA-A61D-46B7-99A6-EC4CA2CDC0BF",
                "name": "test1",
                "company": "",
                "availabilityGSMR": "test1",
                "availabilityMobile": "",
                "availabilityLandline": "test1",
                "availabilityPhoneInternal": "",
                "availabilityPhoneFallback": ""
            }
        ],
        "resonsiblePersonTwo": [
            {
                "uuid": "0414F81E-7F81-468D-BF1B-B1FA8FC34563",
                "name": "test3",
                "company": "",
                "availabilityGSMR": "",
                "availabilityMobile": "",
                "availabilityLandline": "",
                "availabilityPhoneInternal": "",
                "availabilityPhoneFallback": ""
            }
        ],
        "resonsiblePersonFour": null,
        "resonsiblePersonThree": null
    }

    const flatResponsiblePersonObj = _.flatMapDeep(copyResponsiblePerson);
    _.forEach(flatResponsiblePersonObj, (value, key)=> {
      // if any boolen value set to true, therefore responsible form filed is not empty
      if (value !== null) {
        return false;
      }

      if (value.uuid) {delete value.uuid }
      if( _.isObject(value)) {
        if(!_.isEmpty(value)) {
          return false;
        }
      }
    });

    return true;
  }

// Deep copy
var isEmpty = isResponsiblePersonEmpty();
console.log(isEmpty)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Kazi
  • 1,461
  • 3
  • 19
  • 47
  • You could return value instead for true and null for false? – BGPHiJACK Jul 25 '22 at 08:16
  • Have you actually checked what `value` is, if it's not actually `null`? – Dropout Jul 25 '22 at 08:17
  • correct if i'm wrong but isn't the return statement just inside the forEach loop and you're always returning true – Dumitru Birsan Jul 25 '22 at 08:18
  • i am using lodash and in the documentation it said if i return to false then foreach loop should break the iteration @DivineSoul – Kazi Jul 25 '22 at 08:35
  • 2
    Why would you expect anything other than `true` for the function? As you yourself say `return false` will terminate the `_.forEach()` - it's acting like a `break`. But stopping a loop still doesn't stop the function. If you had `function foo() { for(let i = 0; i < 10; i++) { break; } return true; }` the result would still be `true`. – VLAZ Jul 25 '22 at 10:27

0 Answers0