Home > Mobile >  How do I filter based on matching keyword?
How do I filter based on matching keyword?

Time:03-03

How do I filter if header and content contains a matching keyword (example: login), and extract the tabs that have that keyword but categoryName will still be there?

export cons Tab = {
  categoryName: "Account Management",
  tabs: [
    {
      header: "How to login?",
      content: "You need to go to the portal..",
    },
    {
      header: "How to forget password?",
      content: "Click on "Forgot password",
    },
  ]
}

Example output when keyword is "login":

export cons Tab = {
  categoryName: "Account Management",
  tabs: [
    {
      header: "How to login?",
      content: "You need to go to the portal..",
    },
  ]
}

CodePudding user response:

 const Tab = {
  categoryName: "Account Management",
  tabs: [
    {
      header: "How to login?",
      content: "You need to go to the portal..",
    },
    {
      header: "How to forget password?",
      content: "Click on Forgot password",
    }
  ]
}
 
const filterWithKeyword=(keyword)=>{
  Tab.tabs= Tab.tabs.filter(t => t.header.indexOf(keyword)!==-1);
}

filterWithKeyword("login");
console.log(Tab);

We can also use indexOf instead of includes .

CodePudding user response:

You could use the filter method from JavaScript, like so:

 const Tab = {
  categoryName: "Account Management",
  tabs: [
    {
      header: "How to login?",
      content: "You need to go to the portal..",
    },
    {
      header: "How to forget password?",
      content: "Click on Forgot password",
    }
  ]
}
 
const filterWithKeyword=(keyword)=>{
  Tab.tabs= Tab.tabs.filter(t => t.header.includes(keyword));
}

filterWithKeyword("login");
console.log(Tab);

  • Related