如何获取requesterPractitionerId和performerOrganizationId的唯一值。我需要在单个数组中探索requesterPractitionerId和performerOrganizationId的唯一值。
[
{
id: '1043120',
requesterPractitionerId: '1043119',
},
{
id: '1043081',
requesterPractitionerId: '1043080'
},
{
id: 'e1dceebe-c5ba-46a5-a63a-bff709896af4',
requesterPractitionerId: 'e0a844e4-6c8a-489a-8bd6-1d62267d311e',
performerOrganizationId: '05D0889009',
},
{
id: '2709842f-41e3-4193-8607-fc34d3d24ec1',
requesterPractitionerId: 'e0a844e4-6c8a-489a-8bd6-1d62267d311e',
performerOrganizationId: '05D0889009'
}
]
预期产量:
1043119
1043080
e0a844e4-6c8a-489a-8bd6-1d62267d311e
05D0889009
我是Java语言的新手,并为此奋斗了两个小时。任何帮助将不胜感激。
最佳答案
使用Set,Array.reduce和Array.from
使用reduce
创建具有所述字段的唯一值的集合
然后使用Array.from
将集合转换为数组
let arr = [{id:'1043120',requesterPractitionerId:'1043119'},{id:'1043081',requesterPractitionerId:'1043080'},{id:'e1dceebe-c5ba-46a5-a63a-bff709896af4',requesterPractitionerId:'e0a844e4-6c8a-489a-8bd6-1d62267d311e',performerOrganizationId:'05D0889009'},{id:'2709842f-41e3-4193-8607-fc34d3d24ec1',requesterPractitionerId:'e0a844e4-6c8a-489a-8bd6-1d62267d311e',performerOrganizationId:'05D0889009'}];
let result = Array.from(arr.reduce((a,c) => {
if(c.requesterPractitionerId) a.add(c.requesterPractitionerId);
if(c.performerOrganizationId) a.add(c.performerOrganizationId);
return a;
}, new Set()));
console.log(result);