我如何使用Ifs编写此函数以具有相应的else语句

时间:2018-10-26 05:25:52

标签: javascript reactjs

请问我如何用Ifs编写此函数以具有相应的else语句。

我想做的是为每个if语句添加一个else语句

请在下面查看我的代码。

validate = (lname, fname, email, eid) => {
    if (lname) {
        this.setState({
            validationState: {
                ...this.state.validationState,
                lastname: false,
            },
        });
    }
    if (fname) {
        this.setState({
            validationState: {
                ...this.state.validationState,
                firstname: false,
            },
        });
    }
    if (helpers.isEmail(email)) {
        this.setState({
            validationState: {
                ...this.state.validationState,
                email: false,
            },
        });
    }
    if (eid) {
        this.setState({
            validationState: {
                ...this.state.validationState,
                eid: false,
            },
        });
    }
}

1 个答案:

答案 0 :(得分:0)

在下面,检查lname,执行if子句或else子句,然后,不管到目前为止发生了什么,都检查fname,依此类推。 那是您要找的东西吗?

validate = (lname, fname, email, eid) => {
  if (lname) {
    this.setState({
        validationState: {
            ...this.state.validationState,
            lastname: false,
        },
    });
  } else {
  ....
  }

  if (fname) {
    this.setState({
        validationState: {
            ...this.state.validationState,
            firstname: false,
        },
    });
  } else {
  ....
  }

  if (helpers.isEmail(email)) {
    this.setState({
        validationState: {
            ...this.state.validationState,
            email: false,
        },
    });
  } else {
  ...
  }

  if (eid) {
    this.setState({
        validationState: {
            ...this.state.validationState,
            eid: false,
        },
    });
  } else {
  ...
  }

}