簡體   English   中英

對象數組比較 2 個元素值

[英]Array of objects compare 2 element values

我正在嘗試進行字段驗證,並且在比較同一對象數組中的 2 個元素值的情況下陷入困境。 有人可以解釋一下它是如何完成的嗎? 我希望在一次迭代中比較密碼和 repPassword 值。

export const fieldsConfig = [
  {
    id: "email",
    label: "email",
    value: "",
    error: false,
    helperText: "email must includes chars: @ and .",
  },
  {
    id: "userName",
    label: "user name",
    value: "",
    error: false,
    helperText: "user name must be min 5 char",
  },
  {
    id: "password",
    label: "password",
    value: "",
    error: false,
    helperText: "password must be min 5 chars",
  },
  {
    id: "repPassword",
    label: "rep-password",
    value: "",
    error: false,
    helperText: "passwords not mutch",
  },
];

這是我的 redux state 我正在嘗試檢查字段值並對每個字段進行驗證:

case FIELDSVALIDATION:
      const validationFields = [...state.fields].map((element, index, arr) => ({
        id: element.id,
        label: element.label,
        value: element.value,
        error:
          element.id === "email" &&
          !/(.+)@(.+){2,}\.(.+){2,}/.test(element.value)
            ? (element.error = true)
            : element.value.length < 5
            ? (element.error = true)
            : (element.error = false),
        helperText: element.helperText,
      }));
      return {
        ...state,
        fields: validationFields,
      }

我正在使用材料 UI 中的輸入字段表單。 這是組件的一部分以及我如何實現它:

const state = useSelector((state) => state.regReducer);
  const dispatch = useDispatch();
  
  return (
    <div className="registration">
      {state.fields.map((element, key) => {
        return (
          <TextField
            key={key}
            id={element.id}
            label={element.label}
            value={element.value}
            error={element.error}
            helperText={element.error ? element.helperText : ""}
            onChange={(e) =>
              dispatch(onChangeHandler(e.target.id, e.target.value))
            }
          />
        );
      })}

您可以嘗試添加自定義 function 搜索 state 中的字段並比較它們的當前值:

   <div className="registration">
      {state.fields.map((element, key) => {
        const generateError = () => {
          if (element.id.includes("password")) {
            const password = state.fields.find(el => el.id == "password");
            const repPassword = state.fields.find(el => el.id == "repPassword");

            if (password.value != repPassword.value) {
              return true;
            } else {
              return false;
            }
          } else return element.error;
        };

        return (
          <TextField
            key={key}
            id={element.id}
            label={element.label}
            value={element.value}
            error={generateError}
            helperText={element.error ? element.helperText : ""}
            onChange={e =>
              dispatch(onChangeHandler(e.target.id, e.target.value))
            }
          />
        );
      })}
    </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM