UNPKG

hd-utils

Version:

A handy utils for modern JS developers

21 lines (19 loc) 731 B
/** * @description function that takes an object and find the path for the passed key or value, you can pass a predicate function. * @example const myObject = { a: 1, b: { a:2, c: 'hello', d: { e: 'world' }, f: [1, 2, 3] } }; console.log(findKeyOrValuePathInObject(myObject, 'e')); // ['b.d.e'] console.log(findKeyOrValuePathInObject(myObject, (key, val) => Array.isArray(val))); // ['b.f'] console.log(findKeyOrValuePathInObject(myObject, "a")); // ["a", "b.a"] */ export default function findKeyOrValuePathInObject(obj: Record<string, any>, keyOrPredicate: string | ((key: string, value: any) => boolean), navigationChr?: string, currentPath?: string): string[];