Can anyone help me transform a flat array into an array of objects with children in JavaScript? The desired output format should look something like this:
//source format
const arrayObjects = [
{ name: "Alice", lastname: "Silva", age: 25, type: "A", city: "São Paulo" },
{ name: "Bruno", lastname: "Pereira", age: 30, type: "A", city: "Belo Horizonte" },
// more objects here
];
//expected format
const arrayOutPut = [
{
age: 65,
type: "A",
children: [
{
age: 35,
type: "A",
// more properties here
}
]
}
]
If you have any suggestions on the most efficient way to achieve this transformation using JavaScript, please let me know. I've attempted solutions involving flatMap and reduce but haven't been successful so far.