在JavaScript项目实践中,我们可能会经常需要移除重复对象的例子,本文通过一个案例来详细解答,并给出了最优解,希望对你有所帮助。

假设有下面这个数组对象,让你来删除重复项:

  1. const books = [

  2. {

  3. name: "My Sister the Serial Killer",

  4. author: "Oyinkan Braithwaite"

  5. },

  6. {

  7. name: "Educated",

  8. author: "Tara Westover"

  9. },

  10. {

  11. name: "My Sister the Serial Killer",

  12. author: "Oyinkan Braithwaite"

  13. }

  14. ];

数组中的第一个对象和最后一个对象是相同的。那么,如果我们想从数组中删除这样的重复对象怎么办?令人惊讶的是,这是一个相当难解决的问题。为了了解原因,让我们来看看如何从一个数组中删除重复的对象,如字符串等平面项的数组中删除重复的对象。

首先,我们先来看一个简单的数组去重

  1. const strings = [

  2. "My Sister the Serial Killer",

  3. "Educated",

  4. "My Sister the Serial Killer"

  5. ];

如果我们想从这个数组中删除任何重复的项目,我们可以使用filter()方法和indexOf()方法来检查任何给定的项目是否是重复的。

  1. const filteredStrings = strings.filter((item, index) => {

  2. // Return to new array if the index of the current item is the same

  3. // as the first occurence of the item

  4. return strings.indexOf(item) === index;

  5. });


因为strings.indexOf(项)总是会返回该项的第一个出现的索引,所以我们可以判断当前在过滤循环中的项是否是重复的。如果是,我们就不返回到由filter()方法创建的新数组中。

对象并不像上面这么简单

这个相同的方法对对象不起作用的原因是,任何2个具有相同属性和值的对象实际上并不被认为是相同的。

  1. const a = {

  2. name: "My Sister the Serial Killer",

  3. author: "Oyinkan Braithwaite"

  4. };

  5. const b = {

  6. name: "My Sister the Serial Killer",

  7. author: "Oyinkan Braithwaite"

  8. };


  9. a === b // false

这是因为比较对象是基于引用而不是结构来进行比较的。在比较对象时,不会考虑两个对象的属性和值是否相同的事实。因此,在一个对象数组中的indexOf(object)总是会返回所传递的对象的索引,即使存在另一个属性和值完全相同的对象。

我的解决方案是

鉴于这些信息,检查两个对象是否具有相同的属性和值的唯一方法就是实际检查每个对象的属性和值。我想出的解决方案是手动检查,但是为了提高性能和减少不必要的嵌套循环,我做了一些改动。

特别是,我做了3件事情

1.只检查数组中的每一个项目和后面的每一个项目,以避免对同一对象进行多次比较

2.只检查未发现与其他物品重复的物品

3.在检查每个属性的值是否相同之前,先检查两个对象是否有相同的键值

下面是最后的解决方法

  1. function removeDuplicates(arr) {


  2. const result = [];

  3. const duplicatesIndices = [];


  4. // Loop through each item in the original array

  5. arr.forEach((current, index) => {


  6. if (duplicatesIndices.includes(index)) return;


  7. result.push(current);


  8. // Loop through each other item on array after the current one

  9. for (let comparisonIndex = index + 1; comparisonIndex < arr.length; comparisonIndex++) {


  10. const comparison = arr[comparisonIndex];

  11. const currentKeys = Object.keys(current);

  12. const comparisonKeys = Object.keys(comparison);


  13. // Check number of keys in objects

  14. if (currentKeys.length !== comparisonKeys.length) continue;


  15. // Check key names

  16. const currentKeysString = currentKeys.sort().join("").toLowerCase();

  17. const comparisonKeysString = comparisonKeys.sort().join("").toLowerCase();

  18. if (currentKeysString !== comparisonKeysString) continue;


  19. // Check values

  20. let valuesEqual = true;

  21. for (let i = 0; i < currentKeys.length; i++) {

  22. const key = currentKeys[i];

  23. if ( current[key] !== comparison[key] ) {

  24. valuesEqual = false;

  25. break;

  26. }

  27. }

  28. if (valuesEqual) duplicatesIndices.push(comparisonIndex);


  29. } // end for loop


  30. }); // end arr.forEach()


  31. return result;

  32. }

如果文章对你有帮助,点个在看,谢谢!

好文和朋友一起看~

限 时 特 惠: 本站每日持续更新海量各大内部创业教程,一年会员只需98元,全站资源免费下载 点击查看详情
站 长 微 信: lzxmw777

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注