Рекурсия есть:
const getNestedProp = (data, key) => Object
.entries(data instanceof Object ? data : {})
.reduce((acc, [ k, v ]) => (
(k === key) && acc.push(v),
acc.push(...getNestedProp(v, key)),
acc
), []);
const ids = getNestedProp(data.flatMap(n => n.children), 'id');
Рекурсии нет:
function getNestedProp(data, key) {
const result = [];
for (const stack = [ data ]; stack.length;) {
const n = stack.pop();
if (n instanceof Object) {
if (n.hasOwnProperty(key)) {
result.push(n[key]);
}
stack.push(...Object.values(n).reverse());
}
}
return result;
}
// или
function getNestedProp(data, key) {
const result = [];
const stack = [];
for (let i = 0, arr = [ data ]; i < arr.length || stack.length; i++) {
if (i === arr.length) {
[ i, arr ] = stack.pop();
} else if (arr[i] instanceof Object) {
if (arr[i].hasOwnProperty(key)) {
result.push(arr[i][key]);
}
stack.push([ i, arr ]);
[ i, arr ] = [ -1, Object.values(arr[i]) ];
}
}
return result;
}