[英]Store the unique values of two properties from array of objects into single array
How to get unique values of requesterPractitionerId & performerOrganizationId. 如何获取requesterPractitionerId和performerOrganizationId的唯一值。 I need to stroe the unique values of requesterPractitionerId & performerOrganizationId in a single array.
我需要在一个数组中使用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'
}
]
Expected Output: 预期产出:
1043119
1043080
e0a844e4-6c8a-489a-8bd6-1d62267d311e
05D0889009
I am new to Javascript and struggling with this for a couple of hours. 我是Javascript的新手,并在几个小时内苦苦挣扎。 Any help would be realy aapreciated.
任何帮助都会得到很好的回应。
Use Set , Array.reduce and Array.from 使用Set , Array.reduce和Array.from
reduce
to create a set with unique values of the said fields reduce
创建具有所述字段的唯一值的集合 Array.from
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);
One solution is to use Array.reduce() to generate a new Set from the properties you need. 一种解决方案是使用Array.reduce()从您需要的属性生成新的Set 。 After that, you can
spread
the set's items inside an array. 之后,您可以将集合的项目
spread
到数组中。
const input = [{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 res = input.reduce((acc, o) => { if (o.hasOwnProperty("requesterPractitionerId")) acc.add(o.requesterPractitionerId); if (o.hasOwnProperty("performerOrganizationId")) acc.add(o.performerOrganizationId); return acc; }, new Set()) console.log([...res]);
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
You can use forEach
to iterate data
and then use Set
to remove the duplicates 您可以使用
forEach
迭代data
,然后使用Set
删除重复项
let data = [{ 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 newArry = []; data.forEach(function(item) { newArry.push(item.requesterPractitionerId); if (item.hasOwnProperty('performerOrganizationId')) { newArry.push(item.performerOrganizationId) } }) let k = Array.from(new Set(newArry)); console.log(k)
Really simple - Set
and map
, reduce
and filter
: 真的很简单 -
Set
和map
, reduce
和filter
:
const 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'}]; const res = [...new Set(arr.map(({ requesterPractitionerId, performerOrganizationId }) => [requesterPractitionerId, performerOrganizationId]).reduce((acc, curr) => acc.concat(curr)).filter(Boolean))]; console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.