Here is a JSON object that needs to be parsed:
var data =JSON.parse('[{"Title":"Test 1","State":"Colorado"},
{"Title":"Test 1","State":"Arizona"},
{"Title":"Test 2","State":"Utah"},
{"Title":"Test 2","State":"Arizona"},
{"Title":"Test 3","State":"Arizona"}]');
The desired output after parsing the data
object should be as follows:
resultData = [{"Title":"Test 1", State: ["Colorado", "Arizona"]},
{"Title":"Test 2", State: ["Utah", "Arizona"]},
{"Title":"Test 3", State: ["Arizona"]}]
There has been an attempt made with the following code:
var resultData = {},
groupBy = "Title";
for (var i = 0; i < data.length; i++) {
if (!resultData[data[i][groupBy]])
resultData[data[groupBy]] = [];
resultData[data[i][groupBy]].push(data[i]);
};
However, the current output is not as expected:
resultData = [{Test 1: [{State: "Colorado"}, {State: "Arizona"}]},
{Test 2: [{State: "Utah"}, {State: "Arizona"}]},
{Test 3: [{State: "Arizona"}]}]
If anyone can provide assistance in achieving the desired output, it would be greatly appreciated.