Hey everyone, I'm looking for some assistance in organizing certain data effectively while keeping performance in mind. I've been struggling with recursive functions and multiple loops, so I simplified the data to grasp the concept better and solve it with your help. Here's what I have:
const data = [
{
id: 'W1',
color: red,
personId: 'P77',
},
{
id: 'W7',
color: yellow,
personId: 'P21',
},
]
const persons = [
{
id: 'P77',
name: 'Peter',
favoriteFoodId: 'FF4',
},
{
id: 'P21',
name: 'John',
favoriteFood: 'FF9',
}
];
const favoriteFood = [
{
id: 'FF9'
food: 'pasta'
description: 'fresh italian pasta from stone oven'
},
{
id: 'FF4'
food: 'banana'
description: 'fresh bananas from the tree'
}
]
I need help merging this data into a flattened structure incorporating all the references. How can I create a generic solution that handles deep nesting and flattens it with all the necessary references? I attempted a solution but ended up looping excessively and it became overwhelming.
I aim to achieve a result similar to this:
const result = [
{
id: 'W1',
color: red,
name: 'Peter',
food: 'banana'
description: 'fresh bananas from the tree'
},
{
id: 'W7',
color: yellow,
name: 'Peter',
food: 'pasta'
description: 'fresh italian pasta from stone oven'
},
]
The result
will be utilized by my table component to render each object as a row