1

In my react app, I have an API that returns data in this format: set.child.child and it can keep on going (set.child.child.set.child.child) depending on how many layers the data return has. I have a string that holds the path of that data and I need a way to get the data based on the string.

So for example, I have a string that holds the path of where I need the data at. const dataPath = "tableData.rows[0].set.child[1].child".

Here's what I can do: I can run

tableData.rows[0].set.child[1].child.map

Here's what I need to do:

dataPath.map

I had thought there would have been a way to convert dataPath to act as a value that could be mapped over. I was thinking you could do something like {dataPath}.map or maybe there was a type of parse I could use but I can't seem to find anything to do this.

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • you can roll your own if you want (split the string to each of it's parts and then follow the path through the object if it exists) or you could just pick up something like [_.get()](https://www.geeksforgeeks.org/lodash-_-get-method/) – Jacob Smit Aug 10 '23 at 00:18

0 Answers0