{"id":12842,"date":"2023-01-02T14:41:42","date_gmt":"2023-01-02T19:41:42","guid":{"rendered":"http:\/\/local.brightwhiz\/?post_type=snippets&p=12842"},"modified":"2023-01-02T14:41:45","modified_gmt":"2023-01-02T19:41:45","slug":"remove-all-duplicates-from-an-array-in-javascript","status":"publish","type":"snippets","link":"http:\/\/local.brightwhiz\/snippets\/remove-all-duplicates-from-an-array-in-javascript\/","title":{"rendered":"Remove all Duplicates From an Array in JavaScript"},"content":{"rendered":"\n
Here is the code that you can use to remove all duplicates from an array in JavaScript.<\/p>\n\n\n\n
const numbers = [1 , 21, 21, 34 ,12 ,34 ,12];\nconst removeRepeatNumbers = array => [... new Set(array)]\nremoveRepeatNumbers(numbers) \/\/ [ 1, 21, 34, 12 ]\n\nconst removeDuplicates = arr => [...new Set(arr)];\n\n\/\/ Remove Duplicates ES6\nconst uniqueArray = oldArray.filter((item, index, self) => self.indexOf(item) === index);\n\nlet myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']\nlet myOrderedArray = myArray.reduce(function (accumulator, currentValue) {\n if (accumulator.indexOf(currentValue) === -1) {\n accumulator.push(currentValue)\n }\n return accumulator\n}, [])\nconsole.log(myOrderedArray); \/\/ [ 'a', 'b', 'c', 'd', 'e' ]<\/code><\/pre>\n","protected":false},"comment_status":"open","ping_status":"closed","template":"","meta":[],"categories":[953,23,25,27,15,16,18],"tags":[328,424,433,449,544,591,635,638],"yoast_head":"\nRemove all Duplicates From an Array in JavaScript [Snippet]<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n