There is at least an array of objects nested in the items object.
And you wish to validate the nested objects too.
So you need a recursive function to test the nested items. You also need to know when the recursion hits an array or an object.
Have a look at getType(), a function to get the type of an item. It's needed because typeof() returns object for both an object or an array. Based on the returned type, validation() (the recursive function) can send the right item to the next recursion until the item is not an array or object. It can then check if it is an empty string. It also could be a boolean, a number or a function but in those cases, they will not contain a string to be tested.
// Your item structure, as seen on the image in question
const items = {
paymentItem: [
{
billingAccountNum: "",
invoiceNumber: ""
}
],
transactionDate: "",
transactionId: ""
};
// Function to get an item type
const getType = (item) => {
let itemType = typeof(item)
if(itemType === "object"){
itemType = Array.isArray(item) ? "array" : "object"
}
return itemType
}
// The recursive function to turn isValid to false if there is one empty string.
const validation = (item) => {
if(getType(item) === "object"){
Object.keys(item).forEach((key) => validation(item[key])) // Recursion here.
return
}
if(getType(item) === "array"){
item.forEach((element) => validation(element)) // Recursion here.
return
}
if(item === ""){
isValid = false
}
}
// Validation variable
let isValid = true
// Validation function call
validation(items)
// Final result
console.log("first test:", isValid)
// ==============
// Let's do a second test where only one property is empty
const items2 = {
paymentItem: [
{
billingAccountNum: "",
invoiceNumber: 84566
}
],
transactionDate: "2023-01-15T00:20:00",
transactionId: "HS9873-1"
};
// Reset the validation variable
isValid = true
// Validation function call
validation(items2)
// Final result
console.log("second test:", isValid)
const invalid = items.find((item) => !item || Object.values(item).find(val => !val));is one way to do it. This is a low quality question, please read all of how to ask before asking additional questions. Do not post code in images, and try to make a minimal reproducible example