0

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"}],
   }
 }
]
  1. Assume input "go", the result should be:
menu = [
   {
    title: "two",
    secondLevel:{
      name:"level",
      children: [{name: "good"}, {name: "night"}],
   }
 }
]
1

1 Answer 1

2

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; }

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.