Validating json data for dynamic form
I have made some changes and added comments for each change.
// add the default array here ------------↓ 
const validateAndFormatdata = (questions = []) =&gt; {
  let errorsExist = false, // &lt;- typo in exist
      // in case you want to add new types
      optionTypes = new Set([&quot;multiple_answer&quot;, &quot;single_answer&quot;]);
      
  // no need to check for || [] because default is added
  // remove the errors property here itself by using destructuring
  const formattedData = questions.map(({ errors, question_options, ...question }, index) =&gt; {
    // because of the destructuring, question is a new object.
    // So, will not mutate the original object
    question.order = index;

    const newErrors = []
    
    // checks if label exists and if it is an empty string
    if (!question.label) {
      newErrors.push(errorReasons.missingLabel)
    }
    
    if (optionTypes.has(question.question_type)) {
      const questionOptions = question_options
                                    .filter(q =&gt; q.label)
                                    .map((option, order) =&gt; ({ ...option, order }));
                                      
      // === 0 is much more clearer to read
      if (questionOptions.length === 0) {
        newErrors.push(errorReasons.noOption)
      } else {
        // no need for [...]. It's a new array
        question.question_options = questionOptions
      }
    }
    // no need for delete question.question_options because it is destructured

    if (newErrors.length &gt; 0) {
      errorsExist = true
      question.errors = newErrors
    }

    return question;
  })
  
  return {
    errorsExist,
    data: formattedData
  }
}