I am looking for a way to deep merge the following data structure into a unique array of objects in Javascript.
I have explored lodash, reducers, etc and can only get as far as uniquely merging the first nested level.
Starting from this format:
[
"/Apparel",
"/Apparel/Jewelry",
"/Apparel/Jewelry/Rings",
"/Apparel/Jewelry/Precious & Semi-Precious Gems & Gemstone Jewelry",
"/Apparel/Jewelry/Rings/Engagement Rings",
"/Apparel/Jewelry/Precious & Semi-Precious Gems & Gemstone Jewelry/Diamond Jewelry"
]
I have arrived at the following data structure:
[
{
"Occasions & Gifts": {}
},
{
"Apparel": {}
},
{
"Apparel": {
"Clothing": {}
}
},
{
"Occasions & Gifts": {
"Special Occasions": {
"Weddings": {}
}
}
},
{
"Apparel": {
"Clothing": {
"Women's Clothing": {
"Dresses": {}
}
}
}
},
{
"Apparel": {
"Clothing": {
"Formal Wear": {}
}
}
},
{
"Apparel": {
"Clothing": {
"Formal Wear": {
"Bridal Wear": {}
}
}
}
},
{
"Occasions & Gifts": {
"Special Occasions": {}
}
},
{
"Apparel": {
"Clothing": {
"Women's Clothing": {}
}
}
}
]
By deep merging these uniquely I should have a data structure I can work with and iterate through to display on front-end.
The ideal output for the above would be:
{
"Occasions & Gifts": {
"Special Occasions": {
"Weddings": {}
}
}
},
{
"Apparel": {
"Clothing": {
"Women's Clothing": {
"Dresses": {}
}
"Formal Wear": {
"Bridal Wear": {}
}
}
}
}
]
or even better something like:
[
{
"title": "Apparel",
"subTerms": [
{
"title": "Jewellery",
"subTerms": [
{
"title": "Rings",
"subTerms": [
{
"title": "Engagement Rings",
"subTerms": []
}
]
}
]
},
{
"title": "Precious & Semi-Precious Gems & Gemstone Jewelry",
"subTerms": [
{
"title": "Diamond Jewelry",
"subTerms": []
}
]
}
]
}
]