Remove Duplicates from Array
Remove Duplicates from Array is a reusable JavaScript snippet. Several methods to remove duplicate values from an array, from simplest to most flexible. The full implementation: // Method 1: Set (simplest) const unique = arr => [...new Set(arr)] // Method 2: filter + indexOf const unique = arr => arr.filter((item, idx) => arr.indexOf(item) === idx) // Method 3: For objects with a key const uniqueByKey = (arr, key) => arr.filter((obj, idx, self) => idx === self.findIndex(t => t[key] === obj[key]) ) // Usage unique([1, 2, 2, 3, 3, 4]) // [1, 2, 3, 4] This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.