简体   繁体   English

将对象数组中两个属性的唯一值存储到单个数组中

[英]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 使用SetArray.reduceArray.from

  • Use reduce to create a set with unique values of the said fields 使用reduce创建具有所述字段的唯一值的集合
  • Then convert the set to array using 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 : 真的很简单 - Setmapreducefilter

 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.

相关问题 如果对象属性相同,则将数组中的两个对象组合到单个对象中,并将唯一属性转换为数组 - Combine two objects in an array into a single object if the object properties are the same and convert the unique properties into an array 根据两个键从对象数组中获取唯一值 - Get unique values from an array of objects based on two keys 比较两个数组对象并找到唯一值 - Compare two array objects and find unique values 从具有唯一值的对象数组中过滤唯一属性(如果存在),然后覆盖该值 - filter unique properties from array of objects with unique values if existing then override the value 从对象数组中提取属性并存储在另一个对象数组中 - extract properties from array of objects and store in another array of objects 查找两个对象数组之间的共同值并存储为对象数组 - Find common values between two array of objects and store as array of objects 如何从一组复杂的对象中获取一组唯一的属性 - How to get an array of unique properties from an array of complex objects 从对象数组中获取唯一值 - Get unique values from array of objects 从对象数组获取唯一值及其数量 - Get unique values and their amounts from an array of objects 从对象数组中获取唯一值 - Get unique values from an array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM