使用groupby进行Json对象格式化

时间:2016-01-23 06:45:56

标签: javascript angularjs json grouping

我试图通过灌输城市名称来格式化json对象。我正在尝试采用一个单独的数组并根据找到的项目移动而未找到构建字符串。我正在使用javascript / angularjs来实现这一目标。

我当前的字符串

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

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

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

我正在努力实现这种格式

ExpectedDataFormat =

[{
"name":"Los Angeles",
"CityData":
[
{"Id":"17","code":"123","startDate":"1/20/2016","endDate":"1/20/2016"},
{"Id":"19","code":"789","startDate":"1/13/2016","endDate":"1/21/2016"}
]},{
"name":"Chicago",
"CityData":
[
{"Id":"18","code":"456","startDate":"1/22/2016","endDate":"1/25/2016"},
]}

}

逻辑我在尝试什么(尝试使用不同的数据)

var array = [
    { "name": "project1", "url": "picture1-1.jpg"},
    { "name": "project1", "url": "picture1-2.jpg"},
    { "name": "project2", "url": "picture2-1.jpg"},
    { "name": "project3", "url": "picture3-1.jpg"},
    { "name": "project1", "url": "picture1-3.jpg"},
    { "name": "project4", "url": "picture4-1.jpg"},
    { "name": "project3", "url": "picture3-2.jpg"},
    { "name": "project1", "url": "picture1-4.jpg"}
];

var separateArray = [];

$.each(array, function (i, item) {   
    var foundItem = false;
    $.each(separateArray, function (y, newItem) {
        if (newItem.name == item.name) {
            if (!(newItem.url instanceof Array)) {
                newItem.url = [newItem.url];
            }
            newItem.url.push(item.url);
            foundItem = true;
        }
    });

    if (!foundItem) {
     separateArray.push(item);   
    }
});

console.log(separateArray);

2 个答案:

答案 0 :(得分:2)

这段代码能满足您的需求吗?

var newarray = [];

CurrentDataFormat.forEach(function(item){
    if(newarray[item.cityName] != undefined){
      var key = item.cityName;
      delete item.cityName;
      newarray[key].CityData.push(item);
  }else{
    var o = {};
    o.name = item.cityName;
    o.CityData = [];
    o.CityData.push(item);
    newarray[o.name] = o;
  }
});

console.log(newarray);

答案 1 :(得分:1)

检查以下代码,

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

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

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


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});
};

ExpectedDataFormat保持您想要的输出

jsFiddle

的工作演示