I have a data array that contains food items and looks like this.
[
{
"itemId": 80001,
"name": "FRENCH FRIES SMALL",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 6,
"slug": "french-fries-small-80001"
},
{
"itemId": 80002,
"name": "FRENCH FRIES MEDIUM",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 7,
"slug": "french-fries-medium-80002"
},
{
"itemId": 80003,
"name": "FRENCH FRIES LARGE",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 8,
"slug": "french-fries-large-80003"
},
{
"itemId": 80052,
"name": "CRINCKLE WEDGES SMALL",
"description": "CRINCKLE WEDGES SMALL",
"price": 7,
"slug": "crinckle-wedges-small-80052",
"sequence": 14
},
{
"itemId": 80053,
"name": "CRINCKLE WEDGES MEDIUM",
"description": "CRINCKLE WEDGES MEDIUM",
"price": 8,
"slug": "crinckle-wedges-medium-80053",
"sequence": 15
},
{
"itemId": 80054,
"name": "CRINCKLE WEDGES LARGE",
"description": "CRINCKLE WEDGES LARGE",
"price": 9,
"slug": "crinckle-wedges-large-80054",
"sequence": 16
},
]
Now I have to loop through that array and if the name has either SMALL, MEDIUM, or LARGE I have to reformat the data so that it should look something like this e.g.
{
"itemId": 80001,
"name": "FRENCH FRIES",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"itemModifiers":[
{
"itemId": 80001,
"name": "FRENCH FRIES SMALL",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 6,
"slug": "french-fries-small-80001"
},
{
"itemId": 80002,
"name": "FRENCH FRIES MEDIUM",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 7,
"slug": "french-fries-medium-80002"
},
{
"itemId": 80003,
"name": "FRENCH FRIES LARGE",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 8,
"slug": "french-fries-large-80003"
}
],
"slug": "french-fries-80001",
"sequence": 8
}
The legacy system of the client was designed poorly and they are requiring an api that properly formats the data into a more granularized format. I was trying to figure out as to how do this. Please take note that the original array has more items than my example and I should loop through each one of them. Should I rebuild the data from scratch? Or is there a better way of doing this while looping through the array?
via Drew Adorador