DEV Community

Discussion on: Find path in given object

Collapse
 
lexlohr profile image
Alex Lohr

Recursion makes for a simpler solution:

const findPath = (obj, path) => {
  const [item, nextPath] = /([^\.]+)\.(.*)/.exec(path)?.slice(1, 3) || [path];
  return nextPath ? findPath(obj?.[item], nextPath) : obj?.[item];
}
Enter fullscreen mode Exit fullscreen mode