Question: I have created a function to recursively find empty values in a nested array.

The function returns the correct value at first but seems to reset it to the input value before returning the result.

What am I missing?

Here is my code:

```js
const obj = [
  {
    mainContact: true,
    contactName: "",
    emailId: "abc@gmail.com",
    contactAddress: [
      {
        addressType: "",
        county: "U.K.",
        postCode: "MK7 6BZ",
        houseFlatNumber: 1
      },
      {
        addressType: "def",
        county: "France",
        postCode: "123MKO",
        houseFlatNumber: "223"
      }
    ],
    phoneDetails: [
      {
        notes: "",
        phoneNumber: "1234567899",
        countryCode: "44",
        priority: "1"
      },
      {
        notes: "Lorem ipsum",
        phoneNumber: "1112223331",
        countryCode: "48",
        priority: "2"
      }
    ]
  }
];

function validObject(obj, isOk) {
  for (var propName in obj) {
    if (typeof obj[propName] === "object") {
      this.validObject(obj[propName], isOk);
    } else if (
      obj[propName] === null ||
      obj[propName] === undefined ||
      obj[propName] === ""
    ) {
      isOk = false;
      break;
    }
  }
  return isOk;
}

console.log(validObject(obj), true);
// This should return false but it returns true although it first hit the return isOk line
// with a false value then set it back to true

```

Any help would be much appreciated.

Thanks.

Answer: 
An alternate approach is to write a more generic function to recursively test validity of object properties given a predicate to test with.

It's not difficult; to my mind it's often simpler than writing the specific function directly. And applying it to a specific case is also simple.

It might look like this:

```js
const isValid = (pred) => (obj) =>
  Object .values (obj) .every (
    v => (v && typeof v == 'object') ? isValid (pred) (v) : pred (v)
  )

const noEmptyProps = isValid (v => v !== '' && v != null)

const obj = [{mainContact: true, contactName: "", emailId: "abc@gmail.com", contactAddress: [{addressType: "", county: "U.K.", postCode: "MK7 6BZ", houseFlatNumber: 1}, {addressType: "def", county: "France", postCode: "123MKO", houseFlatNumber: "223"}], phoneDetails: [{notes: "", phoneNumber: "1234567899", countryCode: "44", priority: "1"}, {notes: "Lorem ipsum", phoneNumber: "1112223331", countryCode: "48", priority: "2"}]}]

console.log (noEmptyProps (obj))
```
Thanks to James McGuigan, my edited version is as below:

```
       isValidObject(obj) {

         for (var propName in obj) {
            if( typeof obj[propName] === "object" ) {
                if( this.isValidObject(obj[propName]) === false || 
                     obj[propName] === null || 
                     obj[propName].length === 0 ) 
                {
                     return false;
                }
            } 
            else if (
                      obj[propName] === null      || 
                      obj[propName] === undefined || 
                      obj[propName] === '' 
                    ) {
                         return false;  
                      }
            }
            return true;  

        }

```