YuriGor / deepdash

eachDeep, filterDeep, findDeep, someDeep, omitDeep, pickDeep, keysDeep etc.. Tree traversal library written in Underscore/Lodash fashion
https://deepdash.io/
MIT License
272 stars 12 forks source link

deepFilter keep parents #134

Open ziggy6792 opened 2 years ago

ziggy6792 commented 2 years ago

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?