I've written a function that recursively locates empty values in a nested array.
The function initially produces the correct value, but it seems to reset it to the input value before returning the result.
What could I be overlooking?
Below is my code snippet:
const obj = [
{
mainContact: true,
contactName: "",
emailId: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f6e6d6c4f68626e6663216c6062">[email protected]</a>",
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 validateObject(obj, isValid) {
for (var propName in obj) {
if (typeof obj[propName] === "object") {
this.validateObject(obj[propName], isValid);
} else if (
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === ""
) {
isValid = false;
break;
}
}
return isValid;
}
console.log(validateObject(obj), true);
// The expected output should be false, yet it returns true even though it first encountered and set isValid to false
Any assistance would be highly appreciated.
Thank you.