如何根据几个条件从对象数组中过滤重复的对象?

问题描述 投票:0回答:2

我从 API 得到以下响应,我想根据以下条件创建新数组并将结果推送到新数组中

我想根据lineId和totalTime过滤掉所有重复的对象

以下条件进行筛选

例如 - 如果 lineId 相同且两个 lineId 中的总时间之和大于 744,则需要将此对象推送到新数组中。

对于前 - 在下面的响应中,lineId 333 的对象出现了两次,并且它们的总时间总和超过 744, 比我希望这个对象被推入新数组中。

任何人都可以帮我做到这一点吗?

 const response = [
  {  
      "lineId": "333",
      "oeeCalculations": {
          "others": {
              "totalTime": 744
          }
      }
  },
  {
      "lineId": "333",
      "oeeCalculations": {
          "others": {
              "totalTime": 744
          }
      }
  },
  {
      "lineId": "111",
      "oeeCalculations": {
          "others": {
              "totalTime": 744
          }
      }
  },
  {
      "lineId": "111",
      "oeeCalculations": {
          "others": {
              "totalTime": 744
          }
      }
  },
  {
      "lineId": "1115",
      "oeeCalculations": {
          "others": {
              "totalTime": 200
          }
      }
  }
];

预期输出

const result = [
    {  
        "lineId": "333",
        "oeeCalculations": {
            "others": {
                "totalTime": 744
            }
        }
    },
{  
        "lineId": "111",
        "oeeCalculations": {
            "others": {
                "totalTime": 744
            }
        }
    }
   
];

我尝试了下面的代码,但它没有对总时间求和,并且没有按预期工作

const foundDuplicateName = response.find((nnn, index) => {
  return response.find((x, ind) => x.lineId === nnn.lineId && x.oeeCalculations.others.totalTime >= 744 && nnn.oeeCalculations.others.totalTime >= 744 && index !== ind)

})
console.log(foundDuplicateName)
<script>
  const response = [{
      "lineId": "333",
      "oeeCalculations": {
        "others": {
          "totalTime": 744
        }
      }
    },
    {
      "lineId": "333",
      "oeeCalculations": {
        "others": {
          "totalTime": 744
        }
      }
    },
    {
      "lineId": "111",
      "oeeCalculations": {
        "others": {
          "totalTime": 744
        }
      }
    },
    {
      "lineId": "111",
      "oeeCalculations": {
        "others": {
          "totalTime": 744
        }
      }
    },
    {
      "lineId": "1115",
      "oeeCalculations": {
        "others": {
          "totalTime": 200
        }
      }
    }
  ];
</script>

javascript angular typescript angular8
2个回答
0
投票

减少和过滤器可以做到这一点

const filteredArray = Object.values(
  response.reduce((acc, curr) => {
    const { lineId, oeeCalculations: { others: { totalTime } } } = curr;

    acc[lineId] ??= { ...curr, totalTimeSum: 0 }; // Initialize if not already present
    acc[lineId].totalTimeSum += totalTime;

    if (acc[lineId].totalTimeSum > 744) {
      acc[lineId].included = true; // Mark it as valid for inclusion
    }

    return acc;
  }, {})
).filter(item => item.included)
 .map(({ totalTimeSum, included, ...rest }) => rest); // Remove extra fields

console.log(filteredArray);
<script>
const response = [
  { lineId: "333", oeeCalculations: { others: { totalTime: 744 } } },
  { lineId: "333", oeeCalculations: { others: { totalTime: 744 } } },
  { lineId: "111", oeeCalculations: { others: { totalTime: 744 } } },
  { lineId: "111", oeeCalculations: { others: { totalTime: 744 } } },
  { lineId: "1115", oeeCalculations: { others: { totalTime: 200 } } },
];
</script>


0
投票
const duplicateItems = [];
const alreadyAdded = new Set();

response.forEach((item, index, self) => {
    const totalTime = item.oeeCalculations.others.totalTime;
    if (!alreadyAdded.has(item.lineId)) {
    const duplicate = self.find((nItem, nIndex) => 
      nIndex !== index && nItem.lineId === item.lineId &&
      nItem.oeeCalculations.others.totalTime + totalTime > 744
    );

    if (duplicate) {
      duplicateItems.push(item);
      alreadyAdded.add(item.lineId);
    }
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.