JSFiddle - React, Tailwind, and code Playground
HTML
<div id='JSONOutput'></div>
JavaScript
var oldJSON = [{
"categories":"Food",
"subcategories":"Shares",
"pid":"111",
"title":"Smoked Salmon Dip",
"description":"Rich and savory salmon dip, whipped with fresh dill accompanied with croustades"
},
{
"categories":"Food",
"subcategories":"Shares",
"pid":"112",
"title":"Sweet Goat Cheese Flatbread",
"description":"Delicate grilled Naan flatbread coated with delish tomato jam and topped with melted goat cheese, roasted garlic, fennel, onion, pear, shiitake mushroom and arugula."
},
{
"categories":"Food",
"subcategories":"Snacks",
"pid":"100",
"title":"Beer Chili",
"description":"Hot & satisfying short rib chili with black beans, smoked jalapenos, and fresh corn. Topped with aged cheddar cheese and sour cream."
}];
Object.prototype.groupBy = function(itemName, preGrouped)
{
if(preGrouped)
{
for(prop in this)
{
if(typeof(this[prop]) === 'object' && this[prop].groupBy !== undefined)
{
var reGroup = this[prop].groupBy(itemName);
this[prop] = reGroup;
}
}
return this;
}
else
{
var uniqueItems = {};
var uniqueItemLength = 0;
for(var i=0, length=this.length; i < length; i++)
{
var item = this[i];
var z =0;
var found = false;
while(z < uniqueItemLength)
{
if(item[itemName] in uniqueItems)
{
uniqueItems[item[itemName]].push(item);
found = true;
break;
}
z++;
}
if(!found)
{
uniqueItems[item[itemName]] = [];
uniqueItems[item[itemName]].push(item);
uniqueItemLength++;
}
}
return uniqueItems;
}
}
//Run conversion on object
var convertedData =...