Skip to content

Instantly share code, notes, and snippets.

@No-Eul
Last active June 30, 2024 06:02
Show Gist options
  • Save No-Eul/ce773f48eeaff5e8207d9926b78639b4 to your computer and use it in GitHub Desktop.
Save No-Eul/ce773f48eeaff5e8207d9926b78639b4 to your computer and use it in GitHub Desktop.
function find(object, predicate, excludes, thresholdDepth) {
let result = new Map;
function find0(path, object, result, depth) {
if (depth >= thresholdDepth) return;
for (let key of Object.getOwnPropertyNames(object)) {
let value, childPath = [...path, key];
try {
value = object[key];
} catch (key) {
return;
}
if (excludes.some(test => test(key, value))) continue;
if (predicate(key, value)) result.set(childPath, value);
if (value instanceof Object)
find0(childPath, value, result, depth + 1);
}
}
find0("", object, result, 0);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment