objName is an object that contains properties, some of which are arrays.
I’m trying to check that the properties and elements of the obj are not undefined, null or have empty string.
function fieldChecker(obj) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
// Check if the value is an array
if (Array.isArray(value)) {
const hasInvalidElement = value.some(function(element) {
return element === undefined || element === null || element === '';
});
if (hasInvalidElement || key === undefined || key === null || key === '') {
console.log('-', key, ' or its elements are missing or empty. Key Value: ', key, ' Array Value: ', value);
}
} else {
// For non-array values, perform the regular check
if (key === undefined || key === null || key === '' || value === undefined || value === null || value === '') {
console.log('-', key, ' is missing or empty. Key Value: ', key, ' Value: ', value);
}
}
}
}
}
fieldChecker(objName);
I have intentionally altered an array so I can see that the value is an empty sting but this code is not picking it up. I’m fairly new to JS so am not sure I completely understand how to target the elements for error handling.