【问题标题】:Yup - how to check if all arrays are of the same length是的 - 如何检查所有数组是否具有相同的长度
【发布时间】:2021-10-11 23:02:48
【问题描述】:

TLDR:如何检查所有 3 个数组的长度是否相同

我有一个可选对象people,其中有一个数组person_name,每个人都有好恶。 我该如何检查:

如果一个人被输入例如在索引 1,他必须索引 1 有一个数组喜欢的和不喜欢的一样吗?

例如这是被接受的:

{
    person_name: ['foo', 'bar'],
    likes: [['foo stuff'], ['bar stuff']],
    dislikes: [['bar shit'], ['foo shit']]
}

虽然这些不被接受:

{
    person_name: ['foo', 'bar'],
    likes: [['foo stuff'], ['bar stuff']],
    dislikes: [['bar shit']]
}

{
    person_name: ['foo'],
    likes: [['foo stuff'], ['bar stuff']],
    dislikes: [['bar shit']]
}

这是 useFormik 的一个例子

const formik = useFormik({
    initialValues: {
      name: "",
      description: "",
      people: {
        //each index in person_name maps to an array at the same index in likes and dislikes
        person_name: [],
        likes: [[]],
        dislikes: [[]],
      },
    },...

这只是一个示例,因此您不必将答案应用于它 提前非常感谢您

【问题讨论】:

标签: reactjs formik yup


【解决方案1】:

简单数组长度

if (array.length < n){...}

检查孩子(数组内的数组)

if (array.child.length < n){...}

对于yup,您需要编写验证函数,检查this

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 2022-12-01
    • 1970-01-01
    • 2021-12-08
    • 2012-05-20
    • 2018-11-10
    • 2014-02-03
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多