如何在 Javascript/AngularJS 中按 Json 对象分组

Posted

技术标签:

【中文标题】如何在 Javascript/AngularJS 中按 Json 对象分组【英文标题】:How to Group By Json object in Javascript/AngularJS 【发布时间】:2021-03-26 19:39:19 【问题描述】:

我有一个包含 zip 条目的 json 对象。在下面的 json 对象中,“Zipcode”键下有 zip 条目,基本上我想按 Zip code 对其进行分组以显示哪个邮政编码属于所有区域 ID。

例如 - 邮政编码 7 属于 zoneIds 12005,12008,12006,12009,邮政编码 12 属于 zoneIds 12004,11001。最终目标是在一些错误消息中显示这一点。

var Json = [
City: "ABC",
ZipCode: "7",
ZoneID: 12008,
ZoneName: "test_TP41",
City: "ABC",
ZipCode: "7",
ZoneID: 12005,
ZoneName: "test_TP4"
,
City: "ABC",
ZipCode: "7",
ZoneID: 12007,
ZoneName: "test_TP456",
City: "ABC",
ZipCode: "7",
ZoneID: 12006,
ZoneName: "test_TP5",
City: "ABC",
ZipCode: "7",
ZoneID: 12009,
ZoneName: "testgrp16",
City: "CDE",
ZipCode: "12",
ZoneID: 12004,
ZoneName: "test_TP2",
City: "CDE",
ZipCode: "12",
ZoneID: 11001,
ZoneName: "test 20201"
]

【问题讨论】:

【参考方案1】:

您可以执行以下操作来获得这样的结构,

obj = 
  ZipCode: [all the objects with this ZipCode],

Json = [
City: "ABC",
ZipCode: "7",
ZoneID: 12008,
ZoneName: "test_TP41",
City: "ABC",
ZipCode: "7",
ZoneID: 12005,
ZoneName: "test_TP4"
,
City: "ABC",
ZipCode: "7",
ZoneID: 12007,
ZoneName: "test_TP456",
City: "ABC",
ZipCode: "7",
ZoneID: 12006,
ZoneName: "test_TP5",
City: "ABC",
ZipCode: "7",
ZoneID: 12009,
ZoneName: "testgrp16",
City: "CDE",
ZipCode: "12",
ZoneID: 12004,
ZoneName: "test_TP2",
City: "CDE",
ZipCode: "12",
ZoneID: 11001,
ZoneName: "test 20201"
]

const res = Json.reduce((acc, curr) => 
  if(acc.hasOwnProperty(curr.ZipCode)) 
    acc[curr.ZipCode].push(curr);
   else 
    acc[curr.ZipCode] = [curr];
  
  return acc;
, );

console.log(res);

如果您愿意,您可以通过这样做仅在数组中插入 ZoneID,

acc[curr.ZipCode] = [curr.ZoneID];

acc[curr.ZipCode].push(curr.ZoneID);

【讨论】:

感谢您的回答,但现在如何从新构建的对象中获取特定 zip 的信息,例如 7? obj[ZipCode] 就足够了,您会在一个数组中找到所有区域。【参考方案2】:

您可以关注this answer了解更多分类

使用 ES5+,

var grouped = _.mapValues(_.groupBy(Json , 'ZipCode'),
                          singleObject=> singleObject.map(item=> _.omit(Json , 'ZipCode')));

console.log(grouped);

希望对你有帮助

【讨论】:

【参考方案3】:

您可以循环创建一个以 ZipCode 为键、ZoneID 为值的对象

const Json = [
City: "ABC",
ZipCode: "7",
ZoneID: 12008,
ZoneName: "test_TP41",
City: "ABC",
ZipCode: "7",
ZoneID: 12005,
ZoneName: "test_TP4"
,
City: "ABC",
ZipCode: "7",
ZoneID: 12007,
ZoneName: "test_TP456",
City: "ABC",
ZipCode: "7",
ZoneID: 12006,
ZoneName: "test_TP5",
City: "ABC",
ZipCode: "7",
ZoneID: 12009,
ZoneName: "testgrp16",
City: "CDE",
ZipCode: "12",
ZoneID: 12004,
ZoneName: "test_TP2",
City: "CDE",
ZipCode: "12",
ZoneID: 11001,
ZoneName: "test 20201"
];

const ZipMappedObj = 

Json.forEach((k)=>
if(ZipMappedObj[k.ZipCode])
  ZipMappedObj[k.ZipCode].push(k.ZoneID)
 else 
ZipMappedObj[k.ZipCode] = [k.ZoneID]

)
console.log(ZipMappedObj)

【讨论】:

以上是关于如何在 Javascript/AngularJS 中按 Json 对象分组的主要内容,如果未能解决你的问题,请参考以下文章

如何使已选择的选项不显示在另一个选择选项下拉 JAVASCRIPT/ANGULARJs

从笔记本电脑移动到监控 Javascript/AngularJS 时屏幕被切断

从日期我只想在javascript / angularjs中减去1天

Json 对象作为参数传递给 dynamicallay 使用 javascript/angularjs 创建的元素点击事件

javascript AngularJS Redux中间件

javascript AngularJS Redux Dispatch