0

I am trying to check if the element is undefined and if its defined, all the elements inside it are empty, be it an array or nested arry or nested object

Here is the screenshot

https://prnt.sc/n412fbl0aUvv

My try till now

let validation=true;

for(let i=0;i<items.lenght;++i){
  if(Object.keys(items[i]).length===0) {
    validation =false;
    break;
  }
}

its not checking for everything here

1
  • 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 Commented Jan 15, 2023 at 5:22

1 Answer 1

0

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)

Sign up to request clarification or add additional context in comments.

2 Comments

so both are coming as false, why?
Second test was to show it catches an empty string in nested object. I did not made an example where it will output a true because it's too obvious. You can try it right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.