1

TLDR: how do I check that all 3 arrays are of the same length

I have an optional object people, and inside of which is an array person_name and each person has likes and dislikes. How can I check that:

if a person was entered at index 1 for example he must have an array at index 1 inside of likes and the same for dislikes?

for example this is accepted:

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

while these are not accepted:

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

AND

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

this is an example of 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: [[]],
      },
    },...
Community
  • 1
  • 1
Ahmed Nasr
  • 319
  • 5
  • 12

1 Answers1

1

Simple array length

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

Check child(array inside array)

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

For the yup you need to write your validation function, check this

illia chill
  • 1,652
  • 7
  • 11