linqts - 按多个属性分组
linqts - group 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"}];
我想使用 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;
}
}
这是可行的,但我无法弄清楚如何不仅按 componentName 还按 articleNumber 和 componentType 进行分组。当前输出如下所示:
{"CP1": ["V1.1.6"], "CP4": ["V1.1.0"], "CP5": ["V2.1.8", "V2.1.0", "V2.1.8"]}
我喜欢的结果是这样的:
[
{"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"]}
]
您可以收集版本并为每个组构建新的数据集。
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>
我有一个 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"}];
我想使用 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;
}
}
这是可行的,但我无法弄清楚如何不仅按 componentName 还按 articleNumber 和 componentType 进行分组。当前输出如下所示:
{"CP1": ["V1.1.6"], "CP4": ["V1.1.0"], "CP5": ["V2.1.8", "V2.1.0", "V2.1.8"]}
我喜欢的结果是这样的:
[
{"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"]}
]
您可以收集版本并为每个组构建新的数据集。
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>