Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized deep data works #17

Open
YuriGor opened this issue Apr 16, 2020 · 2 comments
Open

Optimized deep data works #17

YuriGor opened this issue Apr 16, 2020 · 2 comments

Comments

@YuriGor
Copy link

YuriGor commented Apr 16, 2020

Hi, it's a very interesting project!
I was curious how deepdash is used here and I suggest to use findValueDeep method here
instead of eachDeep, because findDeep will stop on first found value, while eachDeep will not (until you will ask it)
new code(not tested) may look like

// get the data for the id in the route param if there is one
export const getItemWithId = (data, id) => findValueDeep(data, { id });

note that lodash's iteratee is supported so you can also make a code a bit shorter.

@panphora
Copy link
Collaborator

panphora commented Jun 7, 2021

@YuriGor Thank you for checking Remake out, I'm so happy you find it interesting!
I tried findValueDeep, but it didn't give the result expected.

  1. I need the function to return the parent object, not just the value itself
  2. Also, the code you sent doesn't work as expected, I think it maybe should be: export const getItemWithId = (data, id) => findValueDeep(data, (val, key) => key === "id" && val === id); because the second argument needs to be a function, right?

Anyways, here's the code that works better for me, using findDeep instead:

const getItemWithId = (data, id) => {
  const info = _.findDeep(obj, (val, key) => key === "id" && val === id);
  if (info && info.parent) {
    return info.parent;
  }
};

You can see a demo of it working here: https://codepen.io/panphora/pen/oNZyZJL?editors=0010

Please confirm if this looks okay to you.

@YuriGor
Copy link
Author

YuriGor commented Jun 7, 2021

Hi @panphora!
this issue is 2 years old - like a "hello" from previous life for me, thank you for reply :)

Yes your approach is valid, and my fault was - I left default options unchanged.
Correct suggestion from me should be:
export const getItemWithId = (data, id) => findValueDeep(data, { id }, {leavesOnly:false});

Here is an example forked from your codepen.

Second argument may be not only function but also query in the form string, array or object,
as supported by Lodash iteratee (I just use Lodash iteratee internally)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants