Cómo determinar si un objeto de matriz tiene un valor y luego devolver la matriz de objetos con este valor. Como soy un novato, similar a la búsqueda difusa, da un ejemplo:
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"}], } } ]
Si la estructura de ese objeto es consistente, entonces puede hacer un filter
con some
e 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; }