Skip to content

deepFilter keep parents #134

@ziggy6792

Description

@ziggy6792

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions