const find = (data, key) => Object
.entries(data instanceof Object ? data : {})
.reduce((found, [ k, v ]) => found ?? (k === key ? v : find(v, key)), null);
function find(data, key) {
for (const stack = [ data ]; stack.length;) {
const n = stack.pop();
if (n?.hasOwnProperty(key)) {
return n[key];
}
stack.push(...Object.values(n ?? {}));
}
return null;
}
const findByKey = (object, key) => {
if (object instanceof Array) {
for (let i = 0; i < object.length; i++) {
let result = findByKey(object[i], key);
if (result !== null) {
return result;
}
}
return null;
}
for (let p in object) {
if (p === key) {
return object[p]
} else if (typeof object[p] === 'object') {
let result = findByKey(object[p], key);
if (result !== null) {
return result;
}
}
}
return null;
}
findByKey(someObject, someKey)