Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deepFilter keep parents #134

Open
ziggy6792 opened this issue Jul 19, 2022 · 0 comments
Open

deepFilter keep parents #134

ziggy6792 opened this issue Jul 19, 2022 · 0 comments

Comments

@ziggy6792
Copy link

ziggy6792 commented Jul 19, 2022

Hi

I want to use search an array and keep an item in the array if it or any of its children contain my search string

Example data

const data = [
  { name: 'Simon', children: [{ name: 'Simon' }] },
  { name: 'Ben', children: [{ name: 'Fred' }] },
];

I tried with the following function

const filter = (searchString) => filterDeep(
        data,
        (value: any, key: string, parent: any) => {  
          return value.toString().toLowerCase().includes(searchString.toLowerCase());
        }
      );

filter('Simon') returns

[
  { name: 'Simon', children: [{ name: 'Simon' }] },
]

Which is what I want

filter('Fred') returns

[
  { children: [{ name: 'Fred' }] },
]

But I want

[
   { name: 'Ben', children: [{ name: 'Fred' }] },
]

When a child is found I want its parent/parents to be included in the result

Is this possible with deepdash?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant