【问题标题】:Store the unique values of two properties from array of objects into single array将对象数组中两个属性的唯一值存储到单个数组中
【发布时间】:2019-05-22 05:19:12
【问题描述】:

如何获取 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

我是 Javascript 新手,为此苦苦挣扎了几个小时。任何帮助都会非常感激。

【问题讨论】:

    标签: javascript arrays ecmascript-6 set javascript-objects


    【解决方案1】:

    使用SetArray.reduceArray.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);

    【讨论】:

    • 其实我是用do while循环来获取数据的。那么如何将数据存储在单个数组中并检查数组中的重复条目。你能检查一下代码吗? pastebin.com/S7Sv6s1M
    • 您需要将let newArry = []; 移到do-while 循环之外。有了它在循环中,对于每个循环,以前的循环值都会丢失
    • 你能给我看看代码吗?请检查 pastebin 中的更新代码:pastebin.com/EPb8kBPp
    • ReferenceError: 记录未定义错误抛出
    【解决方案2】:

    一种解决方案是使用Array.reduce() 从您需要的属性中生成一个新的Set。之后,您可以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;}

    【讨论】:

    • 其实我是用do while循环来获取数据的。那么如何将数据存储在单个数组中并检查数组中的重复条目
    【解决方案3】:

    您可以使用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)

    【讨论】:

    • 你能检查一下我在do while循环中写的代码吗? pastebin.com/S7Sv6s1M 但我得到了不同的结果。
    【解决方案4】:

    真的很简单——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; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 2019-05-05
      • 2019-09-02
      相关资源
      最近更新 更多