Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mjapanwala/44b75380c399942475c5232b0dfbb6d2 to your computer and use it in GitHub Desktop.
Save mjapanwala/44b75380c399942475c5232b0dfbb6d2 to your computer and use it in GitHub Desktop.
help.
const menuAndCategory = [
{
menu_name: "First Menu",
menu_description: "I love menus and everything it brings me ",
menu_unique_menu_id: "7d491498-8702-41ba-af53-4dff90f46fc8",
category_name: "Breakfast",
unique_category_id: "71f317cb-6376-4435-ba6c-3a57d006bca6",
product_id: "eda57dcf-234f-423a-9ee3-433e8264ab91",
},
{
menu_name: "First Menu",
menu_description: "I love menus and everything it brings me ",
menu_unique_menu_id: "7d491498-8702-41ba-af53-4dff90f46fc8",
category_name: "Breakfast",
unique_category_id: "71f317cb-6376-4435-ba6c-3a57d006bca6",
product_id: "b39bede4-d02e-46c2-a1d5-ba04c3389f4d",
},
{
menu_name: "First Menu",
menu_description: "I love menus and everything it brings me ",
menu_unique_menu_id: "7d491498-8702-41ba-af53-4dff90f46fc8",
category_name: "Dinner",
unique_category_id: "5a20739e-5614-4166-9f46-64a285e006a0",
product_id: "eda57dcf-234f-423a-9ee3-433e8264ab91",
},
{
menu_name: "First Menu",
menu_description: "I love menus and everything it brings me ",
menu_unique_menu_id: "7d491498-8702-41ba-af53-4dff90f46fc8",
category_name: "Dinner",
unique_category_id: "5a20739e-5614-4166-9f46-64a285e006a0",
product_id: "b39bede4-d02e-46c2-a1d5-ba04c3389f4d",
},
];
const categoryAndProduct = [
{
category_id: "71f317cb-6376-4435-ba6c-3a57d006bca6",
product_name: "Cinnabun Product",
product_description:
"i love so many of these different types of products",
product_ingredients: "Tasty and fun",
unique_product_id: "eda57dcf-234f-423a-9ee3-433e8264ab91",
product_s3_link:
"https://halalbucket21710-dev.s3.amazonaws.com/public/Screen+Shot+2023-05-04+at+8.36.52+PM.png/3e48b5f4-6f40-4df5-ba8e-122db74df57d",
},
{
category_id: "71f317cb-6376-4435-ba6c-3a57d006bca6",
product_name: "Chocholate",
product_description: "I love chocholate chip",
product_ingredients: "Its made with cocoo and vanilla",
unique_product_id: "b39bede4-d02e-46c2-a1d5-ba04c3389f4d",
product_s3_link:
"https://halalbucket21710-dev.s3.amazonaws.com/public/Screen+Shot+2023-05-04+at+8.36.52+PM.png/3e48b5f4-6f40-4df5-ba8e-122db74df57d",
},
{
category_id: "5a20739e-5614-4166-9f46-64a285e006a0",
product_name: "Cinnabun Product",
product_description:
"i love so many of these different types of products",
product_ingredients: "Tasty and fun",
unique_product_id: "eda57dcf-234f-423a-9ee3-433e8264ab91",
product_s3_link:
"https://halalbucket21710-dev.s3.amazonaws.com/public/Screen+Shot+2023-05-04+at+8.36.52+PM.png/3e48b5f4-6f40-4df5-ba8e-122db74df57d",
},
{
category_id: "5a20739e-5614-4166-9f46-64a285e006a0",
product_name: "Chocholate",
product_description: "I love chocholate chip",
product_ingredients: "Its made with cocoo and vanilla",
unique_product_id: "b39bede4-d02e-46c2-a1d5-ba04c3389f4d",
product_s3_link:
"https://halalbucket21710-dev.s3.amazonaws.com/public/Screen+Shot+2023-05-04+at+8.36.52+PM.png/3e48b5f4-6f40-4df5-ba8e-122db74df57d",
},
];
type dataStructure = {
menu_name?: string;
menu_description?: string;
menu_unique_menu_id?: string;
categories?: {
category_name?: string;
unique_category_id?: string;
products?: {
product_name?: string;
product_ingredients?: string;
product_description?: string;
product_id?: string;
unique_product_id?: string;
product_s3_link?: string;
category_id?: string;
}[];
}[];
};
let completeMenus: dataStructure = {
categories: [],
};
menuAndCategory.forEach((menuAndCategoryItem, menuAndCategoryIndex) => {
if (
menuAndCategoryItem.menu_name &&
menuAndCategoryItem.menu_description &&
menuAndCategoryItem.menu_unique_menu_id &&
menuAndCategoryItem.unique_category_id &&
menuAndCategoryItem.category_name &&
menuAndCategoryItem.product_id
) {
const {
menu_name,
menu_description,
menu_unique_menu_id,
unique_category_id,
category_name,
product_id,
} = menuAndCategoryItem;
categoryAndProduct.forEach(
(categoryAndProductItem, categoryAndProductIndex) => {
const {
category_id,
product_name,
product_description,
product_ingredients,
unique_product_id,
product_s3_link,
} = categoryAndProductItem;
//CHECK to see if category already exists inside the menu
let doesCategoryIdExist =
completeMenus.categories?.findIndex(
(item) =>
item.unique_category_id === category_id,
);
// if it exists
if (
doesCategoryIdExist !== -1 &&
doesCategoryIdExist !== undefined
) {
//and if categories exists
if (completeMenus.categories) {
//find out which category index
const category =
completeMenus.categories[
doesCategoryIdExist
];
if (!Array.isArray(category.products)) {
completeMenus.categories[doesCategoryIdExist].products = [];
}
if (category.products) {
completeMenus.menu_name = menu_name;
completeMenus.menu_description =
menu_description;
completeMenus.menu_unique_menu_id =
menu_unique_menu_id;
category.category_name = category_name;
category.unique_category_id =
unique_category_id;
category.products.push({
product_name,
product_description,
product_ingredients,
unique_product_id,
product_s3_link,
});
}
}
} else {
if (completeMenus.categories) {
completeMenus?.categories.push({
category_name: category_name,
unique_category_id: unique_category_id,
});
const lastIndex = completeMenus.categories.length - 1
if (!Array.isArray(completeMenus.categories[lastIndex].products)) {
completeMenus.categories[lastIndex].products = []
}
completeMenus?.categories[
lastIndex
].products?.push({
product_name,
product_description,
product_ingredients,
unique_product_id,
product_s3_link,
});
}
}
},
);
}
});
console.log(completeMenus);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment