Рекурсия есть:
const findPath = (obj, val) =>
Object.is(obj, val)
? []
: Object.entries(obj instanceof Object ? obj : {}).reduce((found, n) => {
if (!found) {
found = findPath(n[1], val);
if (found) {
found.unshift(n[0]);
}
}
return found;
}, null);
Рекурсии нет:
function findPath(obj, val) {
for (const stack = [ [ obj, [] ] ]; stack.length;) {
const [ n, keys ] = stack.pop();
if (Object.is(n, val)) {
return keys;
}
if (n instanceof Object) {
stack.push(...Object.entries(n).reverse().map(([ k, v ]) => [ v, [ ...keys, k ] ]));
}
}
return null;
}