I have an array of objects taking this shape...
type allRecipes = {
rows: [
{
category: string;
id: number;
owner: string;
recipes_uri: string;
recipes_name: string;
}
];
};
Many recipes have the same recipes_name
and the same category
but with different id
, owner
and recipes_uri
.
I need to collate these into this new shape that will remove some of the duplications and make the data easier to handle.
type recipesCollated = [
{
category: string;
recipes_name: string;
recipes_collection: [
{
id: number;
owner: string;
recipes_uri: string;
}
];
}
];
So I'm trying to loop over allRecipes.rows
then should I use .reduce
I've stubbed out some sudo code in the comments...
const recipesCollated = [];
for (let i = 0; i < allRecipes.rows.length; i++) {
// is allRecipes.rows[i].recipes_name in the recipesCollated array??;
// if its not push a new record in with one item in the recipes_collection array
// if it is, loop over recipesCollated.recipes_collection and check to see if the current id is in the array
// if it is, our job is done, if its not insert it into recipesCollated.recipes_collection array
}