0

I have the following items. I want to remove the duplicate items and return the array. I have tried using Set but I think that is not a part of the Ecma script that I am currently using. I know this question has been asked multiples times here but I cannot seem to get mine to work.

0: (2) [0, 2]

1: (2) [0, 2]

2: (2) [1, 2]

3: (2) [1, 3]

 function checkDuplicate(array: any, obj: any) {
    const exists = array.some((o: any) => o.itemOne === obj.itemOne && o.itemTwo === obj.itemTwo);
    if (exists) {
      return true;
    } else {
      return false;
    }
  }

  function check() {
    const testArray: any = [];
    arrayOne().map((item: any) => {
      arrayTwo().map((item2: any) => {
        if (item.someMatchingValue === item2.someMatchingValue) {
          if (!checkDuplicate(testArray, [item.itemOne, item2.itemTwo])) {
            testArray.push([item.itemOne, item2.itemTwo]);
          }
        }
      });
    });
    console.log(testArray);
    return testArray;
  }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Maddy
  • 2,025
  • 5
  • 26
  • 59
  • Possible duplicate of [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Nidhin Joseph Sep 24 '19 at 03:40

2 Answers2

4

You're using const and other ES6 features, so you should be able to use a Set just fine. The issue you probably encountered was that two arrays are not equal to eath other, and so placing your array into a Set won't remove the inner arrays. Instead, you can map each inner array in your array to a string such that you can then use a Set to remove duplicates, and then use Array.from with JSON.parse to convert your Set of strings back into an array of arrays like so:

const arr = [[0, 2], [0, 2], [1, 2], [1, 3]];

const res = Array.from(new Set(arr.map(JSON.stringify)), JSON.parse);
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • I am currently developing in react. Will this work in react aswell? Sorry I should have tagged Reactjs but thought JS was the right tag. – Maddy Sep 24 '19 at 03:45
  • @Maddy I'm not a reactjs expert, but I would say that there wouldn't be an issue with this working in react. – Nick Parsons Sep 24 '19 at 03:46
  • @NickParsons An edge-case would be the use of `JSON.stringify` + `JSON.parse` converting unequal values (like `undefined` and `null`) to the same, equal value (`null`). Granted, I'm not sure that's a case that needs to be accounted for here. – Tyler Roper Sep 24 '19 at 03:47
  • 1
    @TylerRoper oh yeah, I didn't think about that case. I guess it is good to be aware of though. Thanks for pointing that out – Nick Parsons Sep 24 '19 at 03:49
  • 1
    Yes this would work in React and this is a very elegant solution. – Chris Ngo Sep 24 '19 at 03:50
0

I have this utility function I use to reduce an array to distinct elements

const distinct = (array) =>
  array
    ? array.reduce((results, item) => {
        if (!results.some(i => i === item)) {
          results.push(item);
        }
        return results;
      }, [])
    : array;
    
    
let array = [1 ,1 , 2, 3, 3, 4, 5, 5, 5];
console.log(distinct(array));

Seeing you want to compare arrays you could modify it with my object compare utility function

const compare = (obj1, obj2) =>
  Array.isArray(obj1)
    ? Array.isArray(obj2) && obj1.length === obj2.length && obj1.every((item, index) => compare(item, obj2[index]))
    : obj1 instanceof Date
    ? obj2 instanceof Date && obj1.getDate() === obj2.getDate()
    : obj1 && typeof obj1 === 'object'
    ? obj2 && typeof obj2 === 'object' &&
      Object.getOwnPropertyNames(obj1).length === Object.getOwnPropertyNames(obj2).length &&
      Object.getOwnPropertyNames(obj1).every(prop => compare(obj1[prop], obj2[prop]))
    : obj1 === obj2;

const distinct = (array) =>
    array
      ? array.reduce((results, item) => {
          if (!results.some(i => compare(i, item))) {
            results.push(item);
          }
          return results;
        }, [])
      : array;

  let array = [[0, 2], [0, 2], [1, 2], [1, 3]];
  console.log(distinct(array));
 
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60