DEV Community

Discussion on: From if/switch to mapped functions (a.k.a. object lookup)

Collapse
 
clintongallagher profile image
Clinton Gallagher

I have a use case that requires fetching ~400 name=value pairs of lat lon geocordinates from the U.S. Census Bureau API. I need to store the values to be accessed on an as-needed basis such that one pair of geocordinates can be retrieved and used as keys submitted to a weather API.

Would Object lookup be suitable for this?

Collapse
 
ddialar profile image
Dailos Rafael Díaz Lara

If I've understood right, you want to use the pair lat-lon as key in order to apply the lookup procedure.

Thinking that's correct, the process shown in this post is not the most accurate for that because you are going to create the key as string so every time you want to find any specific location, your code has to parse the lat-lon object to string and then, searching for it in the mapped object. I think that "it's not the way" ;)

In opposite, I propose you use to give a try to Map object (MDN documentation) which provides the option to use plain objects as keys and looking for using that objects. That should provide you flexibility and automation advantages.

🤫 NOTE: the second part of this post where I cover the use of Map and TypeScript in order to keep handling the object lookup, will be published in the coming days 😉

In the other way, if you are worried about the memory management, you can use Weakmap. This post is really interesting: Beyond the basics object vs map by Matthew Aftalion.

I hope my answer has provided some light about your doubt.

Collapse
 
clintongallagher profile image
Clinton Gallagher

Thank you for some insight into this context...