Comments (12)
Hi, @GaborTorma, thank you for attention!
Do you expect any difference between pickByDeep and filterDeep?
(and same for omitByDeep vs filterDeep with reversed criteria in predicate?)
from deepdash.
Sorry, no differences. Lodash results only array and cut the key.
Your solution is good. I close this issue.
from deepdash.
Yeah, filtering deep object in a Lodash way makes not so much sense, but name filterDeep
maybe is confusing for those who expect more similarity with lodash.
I'll think about adding aliases.
from deepdash.
I made the pickByDeep:
_.pickByDeep = (obj, predicate, options) => { const result = _.isArray(obj) ? [] : {} _.eachDeep( obj, (v, k, p, c) => { if (predicate(v, k, p, c)) { _.set(result, c.path, v) } }, options ) return result }
You can check the differences in the following:
https://codepen.io/gabortorma/pen/LYpJyjd
The filterDeep not able to use in nested object property, because it's removed in parent level.
Maybe it works for omit with !predicate. I not tested it.
from deepdash.
My first version with reduce:
function pickByDeep(obj, predicate, options) { return _.pick( obj, _.reduceDeep( obj, (r, v, k, p, c) => { if (predicate(v, k, p, c)) { r.push(c.path) } return r }, [], options ) ) }
But I think the eachDeep is quicker.
from deepdash.
Hi, here is a config for filterDeep to behave as you need:
https://codepen.io/yurigor/pen/oNjPWOQ
Also notice i changed predicate a bit, because deepdash expect 3 possible responses from predicate, true, false and undefined (in general it means yes, no and not sure)
from deepdash.
Ohh, i didn't know the undefined predictive result... Thanx!
Maybe can you provide omitByDeep with filterDeep?
Expected result with same predicate:
[
{
"name": "grand 2",
"children": {
"c2": {
"name": "parent 2.2",
"children": [
{
"name": "child 2.2.1"
},
{
"name": "child 2.2.2"
}
],
},
}
},
{
"name": "grand 2",
"children": {
"c1": {
"name": "nincs",
"valami": 111,
"children": [
{
"name": "child 2.1.1"
},
{
"name": "child 2.1.2"
}
]
},
"c2": {
"name": "parent 2.2",
"children": [
{
"name": "child 2.2.1"
},
{
"name": "child 2.2.2"
}
],
},
}
}
]
;
from deepdash.
My solution of omitByDeep:
omitByDeep = (obj, predicate, options) => { const result = _.cloneDeep(obj) _.eachDeep( obj, (v, k, p, c) => { if (predicate(v, k, p, c)) { _.unset(result, c.path) } }, options ) return result }
from deepdash.
Could you please also create codepen, same as you did for pickByDeep so I'll fork it and suggest how to do this with filterDeep
from deepdash.
Thanx! Same data, same predicate, only different function.
You can use this pen: https://codepen.io/gabortorma/pen/LYpJyjd
from deepdash.
https://codepen.io/yurigor/pen/OJyBLEY?editors=0010
- used your predicate unchanged, but negated.
onTrue.skipChildren
option set to false additionally toleavesOnly
false.
(in your codepen markup or css was broken a bit so I created a new one from template and just copied your code)
from deepdash.
Thanx for your help. The options is perfect. Thanx!
But I found a new problem when we use omiting with filterDeep. I create a new issue.
from deepdash.
Related Issues (20)
- omitDeep changes input on empty array HOT 1
- omitDeep doesn't omit objects HOT 7
- ERR_PACKAGE_PATH_NOT_EXPORTED HOT 3
- how to remove values from json if they exist
- Feature Request Parent path
- filterDeep not working on deeply nested object HOT 2
- Working with promises HOT 2
- Frustrating that things like `pickDeep` and `filterDeep` can only return a nested structure HOT 3
- deepdash-es not working with jest-resolve HOT 9
- deepFilter keep parents
- ESM: IIterateeContext typescript interface not exposed
- findPathDeep doesn't return first Value path
- findDeep or findDeepValue - Return object that contains the key & value
- eachDeep with childrenPath == wrong parent, parent path etc
- Bug in documentation _.findDeep*
- findDeep return types problem HOT 3
- Add contributing docs HOT 1
- findDeep is not a function HOT 1
- explicit return values, why and how does this work HOT 1
- mapValuesDeep mutates the original object if `leavesOnly: false` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepdash.