Метод Object.getOwnPropertyNames() возвращает массив со всеми свойствами (независимо от того, перечисляемые они или нет), найденными непосредственно в переданном объекте.
function max(data, key = n => n) {
const getVal = key instanceof Function ? key : n => n[key];
let result = null;
for (const n of data) {
const val = getVal(n);
result = (!result || result[1] < val) ? [ n, val ] : result;
}
return result?.[0];
}
const { text } = max(arr, n => n.text.length);
const oldest = max(arr, 'age');
const result = arr.flat().map((n, i) => ({ ...n, id: -~i }));
const result = [];
for (const n of arr) {
for (const m of n) {
result.push({
...m,
id: result.length + 1,
});
}
}