Code Monkey home page Code Monkey logo

Comments (12)

YuriGor avatar YuriGor commented on May 29, 2024

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.

GaborTorma avatar GaborTorma commented on May 29, 2024

Sorry, no differences. Lodash results only array and cut the key.
Your solution is good. I close this issue.

from deepdash.

YuriGor avatar YuriGor commented on May 29, 2024

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.

GaborTorma avatar GaborTorma commented on May 29, 2024

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.

GaborTorma avatar GaborTorma commented on May 29, 2024

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.

YuriGor avatar YuriGor commented on May 29, 2024

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.

GaborTorma avatar GaborTorma commented on May 29, 2024

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.

GaborTorma avatar GaborTorma commented on May 29, 2024

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.

YuriGor avatar YuriGor commented on May 29, 2024

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.

GaborTorma avatar GaborTorma commented on May 29, 2024

Thanx! Same data, same predicate, only different function.
You can use this pen: https://codepen.io/gabortorma/pen/LYpJyjd

from deepdash.

YuriGor avatar YuriGor commented on May 29, 2024

https://codepen.io/yurigor/pen/OJyBLEY?editors=0010

  • used your predicate unchanged, but negated.
  • onTrue.skipChildren option set to false additionally to leavesOnly 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.

GaborTorma avatar GaborTorma commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.