0

I tried formatting json group by city names with this fiddle code.

I am trying to group by the same code with city name and city code. Any logic hint would be helpful.

CurrentDataFormat = [{
        "Id": 17,
        "code": "123",
        "cityName": "Los Angeles",
        "cityCode": "LA",
        "startDate": "1/20/2016",
        "endDate": "1/20/2016"
    },

    {
        "Id": 18,
        "code": "456",
        "cityName": "Chicago",
        "cityCode": "CH",
        "startDate": "1/22/2016",
        "endDate": "1/25/2016"
    },

    {
        "Id": 19,
        "code": "789",
        "cityName": "Los Angeles",
        "cityCode": "LA",
        "startDate": "1/13/2016",
        "endDate": "1/21/2016"
    }
]

GroupByJsonFormatData(CurrentDataFormat);

function GroupByJsonFormatData(CurrentDataFormat)
{
var refinedArray = {};

for (i = 0; i < CurrentDataFormat.length; i++) {
    refinedArray[CurrentDataFormat[i].cityName] = refinedArray[CurrentDataFormat[i].cityName] ? refinedArray[CurrentDataFormat[i].cityName] : {};

    refinedArray[CurrentDataFormat[i].cityName].name = CurrentDataFormat[i].cityName;

    refinedArray[CurrentDataFormat[i].cityName].CityData = refinedArray[CurrentDataFormat[i].cityName].CityData ? refinedArray[CurrentDataFormat[i].cityName].CityData : [];
    refinedArray[CurrentDataFormat[i].cityName].CityData.push({
        "Id": CurrentDataFormat[i].Id,
        "code": CurrentDataFormat[i].code,
        "startDate": CurrentDataFormat[i].startDate,
        "endDate": CurrentDataFormat[i].endDate
    });
}

var ExpectedDataFormat = [];

for (singleCityName in refinedArray){
    ExpectedDataFormat.push({'name' : refinedArray[singleCityName].name, 'CityData' : refinedArray[singleCityName].CityData});
};

console.log(ExpectedDataFormat);
document.getElementById("resultPane").innerHTML = JSON.stringify(ExpectedDataFormat, undefined, 2);;
}

I also tried to make this code generic by passing column names in parameters. For example I tried like GroupByJsonFormatData(CurrentDataFormat, GroupByColumn); and tried to append this parameter column to array like

refinedArray[CurrentDataFormat[i].+GroupByColumn]

but it didnt work at the concatenation level.

Kurkula
  • 6,386
  • 27
  • 127
  • 202

2 Answers2

1

You can simplify code using:

refinedArray[CurrentDataFormat[i].cityName] = refinedArray[CurrentDataFormat[i].cityName] || {};

instead of

refinedArray[CurrentDataFormat[i].cityName] = refinedArray[CurrentDataFormat[i].cityName] ? refinedArray[CurrentDataFormat[i].cityName] : {};

Same with array:

refinedArray[CurrentDataFormat[i].cityName].CityData = refinedArray[CurrentDataFormat[i].cityName].CityData || [];

For generic implementation you can use

refinedArray[CurrentDataFormat[i]][GroupByColumn]
Viktor Kukurba
  • 1,360
  • 9
  • 14
1

Lodash could help you:

var groupedByCity =  _.groupBy(CurrentDataFormat, "cityName")

Maybe you are looking to renaming the keys or something else togheter

go for this hint or search in docs

Community
  • 1
  • 1
vinjenzo
  • 1,490
  • 13
  • 13