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]; }
We're a place where coders share, stay up-to-date and grow their careers.
Recursion makes for a simpler solution: