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

Frustrating that things like `pickDeep` and `filterDeep` can only return a nested structure #132

Closed robross0606 closed 2 years ago

robross0606 commented 2 years ago

pickDeep and filterDeep can only return a nested structure that matches the original structure. There does not appear to be a way to, for example, pick all the results from a deep structure and return them as a flattened array of values.

dkdev030 commented 2 years ago

Hello @robross0606 ,

i just stumpled over this issue. A few things. First your issue is pretty rude not to say disrespectful formulated. @YuriGor does a really great job and i am sure you have to admit that, too.

Also since Deepdash uses Lodash, there are a lot of easy ways to circumvent your issue and get the desired result.

// 1. use the `condense`-option `filterDeep` or `pickDeep` so that the initilal structure is preserved. 
const deepFilteredResults = _.filterDeep(obj, predicate, { condense: true });

// 2. create the paths out of the result
const paths = _.paths(deepFilteredResults)

// 3. get the result
const values = paths.map((path) => _.get(obj, path)
YuriGor commented 2 years ago

Hi @robross0606 you can use reduceDeep just use array as accumulator and push only values you need. I wanted to implement something like selectDeep to do such things, but I have no need/free time for this. Feel free to open PR if you'd like.

Thanks @dkdev030 yes you suggest would also work but, if preserving structure is not needed - better to not use filterDeep because it spends resources under the hood to preserve such structure.

robross0606 commented 2 years ago

@YuriGor yep, reduceDeep is already precisely what I ended up using. I would have responded sooner, but I purposely took a breather to avoid getting baited by clear trolling from @dkdev030. Thanks for the advice and, yes, the library is clearly useful. 😁