[英]linqts - group by multiple properties
I have an array of JSON objects which have to be grouped by multiple properties. 我有一个JSON对象数组,必须按多个属性进行分组。
const JSON_DATA = [ {"componentName":"CP1","articleNumber":"441","componentType":"Ext","version":"V1.1.6"}, {"componentName":"CP5","articleNumber":"444","componentType":"Int","version":"V2.1.8"}, {"componentName":"CP5","articleNumber":"444","componentType":"Ext","version":"V2.1.0"}, {"componentName":"CP5","articleNumber":"444","componentType":"Ext","version":"V2.1.8"}, {"componentName":"CP4","articleNumber":"442","componentType":"Ext","version":"V1.1.0"}];
I'd like to use linqts to group by componentName, articleNumber and componentType. 我想使用linqts按componentName,articleNumber和componentType分组。
interface IComponent { componentName: String; articleNumber: String; componentType: String; version: String; } class JsonGroupBy { public groupBy(data: IComponent[]): any { let mylist = new List < IComponent > (data); let result = mylist.GroupBy((comp) => comp.componentName, (comp) => comp.version); return result; } }
This is working but I can't figure out how to group not only by componentName but also by articleNumber and componentType. 这是可行的,但我不知道如何不仅按componentName分组,还按articleNumber和componentType分组。 The current output looks like this:
当前输出如下所示:
{"CP1": ["V1.1.6"], "CP4": ["V1.1.0"], "CP5": ["V2.1.8", "V2.1.0", "V2.1.8"]}
My prefered result would be like this: 我更喜欢的结果是这样的:
[ {"componentName": "CP1","articleNumber":"441","componentType":"Ext","version": ["V1.1.6"]}, {"componentName": "CP5","articleNumber":"444","componentType":"Int","version": ["V2.1.8"]}, {"componentName": "CP5","articleNumber":"444","componentType":"Ext","version": ["V2.1.0","2.1.8"]}, {"componentName": "CP4","articleNumber":"442","componentType":"Ext","version": ["V1.1.0"]} ]
You could collect the version and build a new data set for each group. 您可以收集版本并为每个组建立一个新的数据集。
const JSON_DATA = [{ componentName: "CP1", articleNumber: "441", componentType: "Ext", version: "V1.1.6" }, { componentName: "CP5", articleNumber: "444", componentType: "Int", version: "V2.1.8" }, { componentName: "CP5", articleNumber: "444", componentType: "Ext", version: "V2.1.0" }, { componentName: "CP5", articleNumber: "444", componentType: "Ext", version: "V2.1.8" }, { componentName: "CP4", articleNumber: "442", componentType: "Ext", version: "V1.1.0" }], result = Enumerable .From(JSON_DATA) .GroupBy( null, "$.version", "{ componentName: $.componentName, articleNumber: $.articleNumber, componentType: $.componentType, version: $$.ToArray() }", "[$.componentName, $.articleNumber, $.componentType].join('|')" ) .ToArray(); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.