Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

remove duplicates by id

interface MyArray {
  id: string;
  name: string;
}

const arr: MyArray[] = [{id: '1', name: 'name'}, {id: '2', name: 'name2'}, {id: '3', name: 'name3'}];


const removeDuplicates = <T extends {id: string}>(array: T[]) =>
  array.reduce((acc: T[], item) => [...acc.filter(({ id }) => id !== item.id), item], []);

removeDuplicates(arr);
 
PREVIOUS NEXT
Tagged: #remove #duplicates #id
ADD COMMENT
Topic
Name
8+8 =