0

The code is as follows

validateItems = (order, action) => {
    let hasError = false;
    orders.map(order => {
      if (this.hasLineItemFormErrors(order, action)) hasError = true;
    });
    return hasError;
  };
hasLineItemFormErrors = (order, action) => {
    let hasError = false;
    this.getLineItemsForms(order.id).map(form => {
      if (form.hasError) hasError = true;
    });
    return hasError;
  };

As you can see, i have to repeat the hasError variable on each method. Is there a more cleaner way to write this?

Something like

hasLineItemFormErrors = (order, action) => {
        return this.getLineItemsForms(order.id).map(form => {
          if (form.hasError) return true;
        }); // When there are no errors, this will return formsArray,  so the hasError variable in parent method becomes true
      };
0

2 Answers 2

2

If I understand correctly, you should use Array.some for that:

const validateItems = (orders, action) => {
    return orders.some(order => this.getLineItemsForms(order.id).some(form => form.hasError))
}

This will return true or false if one of the orders has at least one error.

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

Comments

0

It looks like you can get rid of both the methods and use Array.some ?

validateItems = (order, action) => {
    return orders.some(order => this.hasLineItemFormErrors(order, action))
};
hasLineItemFormErrors = (order, action) => {
    return this.getLineItemsForms(order.id).some(form => form.hasError);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.