使用 groupby 格式化 Json 对象
Json object formatting with a groupby
我正在尝试通过对城市名称进行分组来格式化 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);
这段代码会做你需要的吗?
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);
检查以下代码,
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的工作演示
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 我应该如何将响应数据保存在对象(json)中以获得更好的操作和性能
- 用javascript从列表对象(JSON的)构建diffrent选项卡
- php请求带有多个对象json-jquery
- AngularJS,过滤器:如何将一个巨大的对象(JSON)变成一个数组
- 动态属性对象 - JSON
- JavaScript 对象 (JSON) 中的动态设置值
- 从数组查询获取对象 JSON 值时出错
- 使用jQuerygetJSON将多维对象JSON转换为HTML
- 需要从 Angular JS 应用程序中的另一个 JSON 对象数组填充 JSON 数组的每个对象 JSON 对象数组
- 嵌套对象JSON排序JavaScript
- PHP在MySQL中保存来自Javascript的对象"JSON.stringify"通过Ajax
- 将JavaScript对象/ JSON转换为PHP数组
- JSON对象.JSON对象内部
- 访问对象的对象- JSON API
- 数组到对象JSON
- Message":"传入的无效对象(JSON数据格式化问题)