const someObj = {
title: 1,
children: {
name: 'test',
title: 2,
children: {
title: 3,
}
}
}
const getLast = (obj, parentName) =>
obj.next instanceof Object
? getLast(obj.next, obj.name)
: { ...obj, parentName };
function getLast(obj) {
let parentName = void 0;
for (; obj.next; parentName = obj.name, obj = obj.next) ;
return { ...obj, parentName };
}