Created
November 28, 2018 01:30
-
-
Save technowar/35a66343e4b65ff1249c68b4f9a88655 to your computer and use it in GitHub Desktop.
ES6 remove dup in an array of object by checking `id` property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* 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