The topath
function from lodash can be used to convert a string into a path array. A path array is an array of all the keys in an object's nested properties that can be used to access a corresponding value. Here's an example of how to use it:
index.tsx243 chars17 lines
In the code above, we first require lodash to have access to the toPath
function. We then create an object with a nested value. We define a string representing a path to the nested value, and then use the toPath
function to convert the string into an array of keys. Finally, we use lodash's get
function to retrieve the nested value using the path array.
This code would output 123
to the console because we successfully retrieved the nested value using the get
function with the pathArr
array we created using toPath
.
gistlibby LogSnag