【问题标题】:Get list of duplicate objects in an array based on two property in javascript基于javascript中的两个属性获取数组中重复对象的列表
【发布时间】:2020-09-09 17:39:01
【问题描述】:

我正在尝试根据 2 个属性在数组中获取重复的对象。假设对象如下所示。

let arry = [
    {Level: "A-1", Status: "approved"},
    {Level: "A-2", Status: "approved"},
    {Level: "A-3", Status: "approved"},
    {Level: "A-4", Status: "reject"},
    {Level: "A-5", Status: "reject"},
    {Level: "A-1", Status: "approved"},
    {Level: "A-2", Status: "approved"},
    {Level: "A-6", Status: "approved"},
    {Level: "A-7", Status: "reject"},
    {Level: "A-1", Status: "approved"},
    {Level: "A-6", Status: "approved"},
]

应该返回重复的对象,并且只有一次“已批准”状态,如下所示:

[
{Level: "A-1", Status: "approved"},
 {Level: "A-2", Status: "approved"}
]

【问题讨论】:

标签: javascript arrays angular


【解决方案1】:

您可以使用 Array.reduce 在遍历数组时跟踪所有之前的项目,同时只保留之前看到的那些(Status"approved")。

为了不输出重复的重复,它还会检查当前重复是否已经被找到为重复,如果是则忽略它。

const array = [
  { Level: "A-1", Status: "approved" },
  { Level: "A-2", Status: "approved" },
  { Level: "A-3", Status: "approved" },
  { Level: "A-4", Status: "reject" },
  { Level: "A-5", Status: "reject" },
  { Level: "A-1", Status: "approved" },
  { Level: "A-2", Status: "approved" },
  { Level: "A-6", Status: "approved" },
  { Level: "A-7", Status: "reject" },
  { Level: "A-1", Status: "approved" },
  { Level: "A-6", Status: "approved" },
];

const dupes = array.reduce((dupes, { Level, Status }, i) => {
  const matches = ({ Level: l, Status: s }) => l == Level && s == Status;
  const match = dupes.memory.find(matches);
  const found = dupes.dupes.find(matches);
  (match?.Status == 'approved' && !found)
    ? dupes.dupes.push({ Level, Status })
    : dupes.memory.push({ Level, Status });
  return (i == array.length - 1) ? dupes.dupes : dupes;
}, { dupes: [], memory: [] });

console.log(dupes);

一个不太通用但更简洁的策略可能是:

const dupes = [...new Set(array
  .filter(({ Status: s }) => s == 'approved')
  .map(({ Level: l }) => +l.match(/\d+/)[0])
  .filter((num, _, arr) => arr.filter((n) => n == num).length > 1))]
  .map((n) => ({ Level: 'A-' + n, Status: 'approved' }));

例如:

const array = [
  { Level: "A-1", Status: "approved" },
  { Level: "A-2", Status: "approved" },
  { Level: "A-3", Status: "approved" },
  { Level: "A-4", Status: "reject" },
  { Level: "A-5", Status: "reject" },
  { Level: "A-1", Status: "approved" },
  { Level: "A-2", Status: "approved" },
  { Level: "A-6", Status: "approved" },
  { Level: "A-7", Status: "reject" },
  { Level: "A-1", Status: "approved" },
  { Level: "A-6", Status: "approved" },
];

const dupes = [...new Set(array
  .filter(({ Status: s }) => s == 'approved')
  .map(({ Level: l }) => +l.match(/\d+/)[0])
  .filter((num, _, arr) => arr.filter((n) => n == num).length > 1))]
  .map((n) => ({ Level: 'A-' + n, Status: 'approved' }));

console.log(dupes);

【讨论】:

    【解决方案2】:

    let arry = [
        {Level: "A-1", Status: "approved"},
        {Level: "A-2", Status: "approved"},
        {Level: "A-3", Status: "approved"},
        {Level: "A-4", Status: "reject"},
        {Level: "A-5", Status: "reject"},
        {Level: "A-1", Status: "approved"},
        {Level: "A-2", Status: "approved"},
        {Level: "A-6", Status: "approved"},
        {Level: "A-7", Status: "reject"},
        {Level: "A-1", Status: "approved"},
        {Level: "A-6", Status: "approved"},
    ]
    
    function dups(arry) {
      const hashMap = {};
    
      return arry.reduce((acc, curr) => {
        const level = curr.Level;
        if(hashMap[level] && hashMap[level] === 1 && curr.Status === 'approved') {
          acc.push(curr);
          hashMap[level]++;
        } else {
          hashMap[level] = 1;
        }
    
        return acc;
      }, []);
    }
    
    console.log(dups(arry));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2014-09-03
      • 2013-03-13
      • 2012-12-07
      相关资源
      最近更新 更多