As a novice, I am trying to figure out how to convert an array of values into nested objects based on categories. If the category (i.e., 1st element) exists, then the subsequent subcategories should be nested within it. Can someone please assist me with achieving this goal?
For example:
const newCat = [
[
"Grocery", // category
"Food & Drink", // sub-category
"Snacks, Crisps and Sweets", // sub-sub-category
],
[
"Grocery",
"Canned, Dry & Packaged Food",
"Pickled Foods",
],
[
"Grocery",
"Food & Drink",
],
[
"Grocery",
"Food & Drink",
"Nuts, Dates & Dried Fruits",
],
[
"Grocery",
"World Specialities",
"India",
],
]
OUTPUT -
[
{
CategoryName: "Grocery",
SubCategories: [
{
CategoryName: "Food & Drink",
SubCategories: [
{
CategoryName: "Snacks, Crisps, and Sweets",
},
],
},
{
CategoryName: "Canned, Dry & Packaged Food",
SubCategories: [
{
CategoryName: "Pickled Foods",
},
],
},
],
}
]