Object로 구성된 배열에서 중복 제거하기

User [{"id": 1, "name": "testman", "description": "For Test" ... }, ....]와 같이 구성되어 있는 다수의 리턴 값에서 중복을 제거할 일이 있었다. 원래는 Set을 활용하여 중복 요소를 제거하려 하였지만, 리턴 값의 자료형이 User[ ]와 같은 Objects[ ]여서 제거가 되지않았다. 아래와 같이 ES6의 filter( )를 사용하여 중복을 제거하였다.

const User = [{"id": 1, "name": "testman", "description": "For Test" ... }, ....]; // 기본 중복된 User 리스트

const removeDupUser = User.filter((item, index, array) =>
    index === array.findIndex(foundItem => isPropValuesEqual(foundItem, item, propNames))
  ); // 중복 제거된 User 리스트

 

참고) https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects

 

How to remove all duplicates from an array of objects?

I have an object that contains an array of objects. things = new Object(); things.thing = new Array(); things.thing.push({place:"here",name:"stuff"}); things.thing.push({place:"there",name:"more...

stackoverflow.com