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

get function can not retrieve the value from an json object which have complex key name. #441

Open
JongLim107 opened this issue Dec 20, 2024 · 8 comments

Comments

@JongLim107
Copy link

get function can not get value from below json object, but lodash can! 😂

{"/api-v1.2": "some-value"}

Screenshot 2024-12-20 at 22 58 57
@aleclarson
Copy link

aleclarson commented Dec 20, 2024

No need for get in this instance.

const routeCfg = configs[routeName] ?? {}

Lodash uses a complicated and much less performant regex approach, which apparently works in your case because the period . is part of a number 1.5.

Due to its simplified implementation, I would avoid using Radash get in cases where the key may contain periods.

P.S. Check out Radashi, an actively maintained fork of Radash with many improvements and new functions

@JongLim107
Copy link
Author

JongLim107 commented Dec 20, 2024

Why I want to use either radash.get() or loads.get() method is because the configs object can be undefined, I don’t want it to throw an error.

@aleclarson
Copy link

const routeCfg = configs?.[routeName] ?? {}

@JongLim107
Copy link
Author

const routeCfg = configs?.[routeName] ?? {}

How about the key here is a multiple level key of nested objects?

@aleclarson
Copy link

Don't know what you mean. Give an example with input data and expected result. Use triple backticks to quote the code, so I can copy-paste it.

@JongLim107
Copy link
Author

Don't know what you mean. Give an example with input data and expected result. Use triple backticks to quote the code, so I can copy-paste it.

Think of the scenario where you want to use get method to retrieve a json value.

Anw, I just want to report a bug here, not asking for workaround 🤣

@aleclarson
Copy link

It's not really a bug, just a limitation. Something is only a bug if it's a documented behavior that isn't working as expected. You're actually making a feature request.

Semantics aside, if we can't understand your use case, we can't solve your problem. That's where a minimal example helps much more than you'd think.

It's quite possible your expectation is unrealistic and you're using the wrong tool for the job. We're all guilty of such things, now and again.

@JongLim107
Copy link
Author

JongLim107 commented Dec 21, 2024

Ok, you can take this as a feature request, but I believe many people are just like me, we don’t quite understand its subtle limitations before using a “replacement“ function which other libraries can work.

And then why I would like to use get methods?

According to what you said, there are always ways to get the value in the json object without using third-party libraries and functions, just using a lot of conditional statements and non-null judgments to ensure that no errors are thrown.

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