Answer the question
In order to leave comments, you need to log in
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);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question