B
B
baroman4ik2022-02-04 18:09:13
JavaScript
baroman4ik, 2022-02-04 18:09:13

How to correctly implement filtering with two conditions?

There is a function that should filter the contents of the array using filter by two conditions through if and the first condition returns the expected result, but in the second condition, when executed, a new object should return, but returns an iterable element. Please tell me how to bypass this jamb.

function search(value) {
    if (value !== "") {
      setFiteredArray((prev) =>
        prev.filter((theme) => {
          if (theme.name.indexOf(value) !== -1) return theme;
          else if (
            theme.subtopics.some(
              (topic) =>
                topic.name.toLowerCase().indexOf(value.toLowerCase()) !== -1
            ) === true
          )
            return {
              ...theme,
              subtopics: theme.subtopics.filter((topic) =>
                topic.name.toLowerCase().indexOf(value.toLowerCase()) !== -1
                  ? topic
                  : null
              )
            };
          else return null;
        })
      );
    } else {
      setFiteredArray(themes);
    }
  }

the array looks like this: [ {..., name: value, subtopics: [..., name: dfgdfgdfg] } ]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question