如果可以对if算法进行优化

我有以下状态:

state = {
    profile: {
      name: { value: '', isRequired: true, hasError: false, order: 1 },
      industry: { value: '', isRequired: false, hasError: false, order: 2 },
      address: { value: '', isRequired: false, hasError: false, order: 3 },
      crn: { value: '', isRequired: true, hasError: false, order: 4 },
      website: { value: '', isRequired: false, hasError: false, order: 5 },
      employeesNbr: { value: '', isRequired: true, hasError: false, order: 6 },
      phoneNumber: { value: '', isRequired: true, hasError: false, order: 7 },
      userRole: { value: '', isRequired: true, hasError: false, order: 8 },
      personCheck: {
        value: false,
        isRequired: true,
        hasError: false,
        order: 9,
      },
    },
    showModal: {
      modal: { error: false },
    },
  };

我有以下部分的提交处理程序

if (
        !profile.name.hasError &&
        !profile.crn.hasError &&
        !profile.employeesNbr.hasError &&
        !profile.phoneNumber.hasError &&
        !profile.userRole.hasError &&
        profile.personCheck.hasError
      ) {
        showModal.modal.error = true;
      }

我可以使用哪些方法来增强if语句? 谢谢

评论
  • ab_non
    ab_non 回复

    这样你更喜欢吗?

    if (
            !(profile.name.hasError ||
            profile.crn.hasError ||
            profile.employeesNbr.hasError ||
            profile.phoneNumber.hasError ||
            profile.userRole.hasError) &&
            profile.personCheck.hasError
          ) {
            showModal.modal.error = true;
          }