How to determine whether an array object has a value, and then return the array of objects with this value. Because I'm a novice, Similar to fuzzy search, give an example:
const menu = [
{
title: "one",
secondLevel:{
name:"hellobye",
children: [{name: "hello"}, {name: "bye"}],
}
},
{
title: "two",
secondLevel:{
name:"level",
children: [{name: "good"}, {name: "night"}],
}
}
]
menu = [
{
title: "two",
secondLevel:{
name:"level",
children: [{name: "good"}, {name: "night"}],
}
}
]
If that object structure is consistent, then you can just do a filter
with some
and includes
:
const menu=[{title:"one",secondLevel:{name:"hellobye",children:[{name:"hello"},{name:"bye"}],}},{title:"two",secondLevel:{name:"level",children:[{name:"good"},{name:"night"}],}}];
const filterBy = toFilter => {
return menu.filter(({ secondLevel }) => secondLevel.children.some(({ name }) => name.includes(toFilter)));
};
console.log(filterBy("go"));
console.log(filterBy("ll"));
.as-console-wrapper { max-height: 100% !important; top: auto; }