Skip to content

Instantly share code, notes, and snippets.

@technowar
Created November 28, 2018 01:30
Show Gist options
  • Save technowar/35a66343e4b65ff1249c68b4f9a88655 to your computer and use it in GitHub Desktop.
Save technowar/35a66343e4b65ff1249c68b4f9a88655 to your computer and use it in GitHub Desktop.
ES6 remove dup in an array of object by checking `id` property
/***
* Initial array
***/
const arr = [{
name: 'A',
id: 1
}, {
name: 'B',
id: 2
}, {
name: 'C',
id: 3
}];
/***
* Append to array
***/
const newArr = [].concat(...[arr, [{
name: 'D',
id: 3
}, {
name: 'E',
id: 4
}, {
name: 'F',
id: 5
}, {
name: 'G',
id: 6
}, {
name: 'H',
id: 7
}]]);
/***
* Remove dup by checking `id` property
***/
const removeDup = newArr.filter((elem, index, self) => self.findIndex(elemIndex => elemIndex.id === elem.id) === index);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment