I have fairly symmetrical data I want to reduce:
const data = [
{
name: 'Bob',
relations: {
siblings: [
{
name: 'Tom',
age: '20'
},
{
name: 'Jacob'
}
]
}
},
{
name: 'Robert',
relations: {
siblings: [
{
name: 'Timmy',
age: '16'
}
]
}
}
];
What I’m trying to produce:
const output = {
name: ['Bob', 'Robert'],
relations: {
siblings: {
name: ['Tom', 'Jacob', 'Timmy'],
age: ['20', '16']
}
}
}
I understand how to do this without recursion, but I wanted a solution that would go deep.
Usually I just use reduce with recursion, but then I realized that I would have to add the value to the current level of the object which I don’t know how to do.
const compact = (value) => {
if (typeof value === 'string') {
return { [value]: '' }; // turning into an object for the mean time
}
if (Array.isArray(object)) { }
// if object
return Object.entries(object).reduce((accum, [key, value]) => {
// Do I have to pass accum[key] into compact here? Is this the correct way to do this?
accum[key] = compact(value);
return accum;
}, {});
};