In this javascript tutorial, we would like to show you how to remove duplicate values from the array. For this, we will use the javascript filter function to delete duplicates values from the array.
In below method will find duplicates in the array and filter it. This is a very straight forward javascript method to remove duplicates from array
Javascript Remove Duplicates from Array Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<html> <head> <title>javascript remove duplicates from array</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <script type="text/javascript"> var colorArray = ['Red', 'Blue', 'Red', 'Green', 'Blue']; var uniqueArray = colorArray.filter(function(elem, index, self) { return index === self.indexOf(elem); }); console.log(uniqueArray); </script> </body> </html> |
Output:
1 2 3 4 5 |
Array(3) 0: 'Red' 1: 'Blue' 2: 'Green' |
Hope this will help you!