< BACK

Javascript tips, tricks and code snippets

Unique values of an array

Use the native .filter method of an Array in Javascript like this to get the unique values:

let arr = [
  'google',
  'mail',
  'smtp',
  'exim4',
  'ubuntu',
  'authentication',
  'google', // repeated
  'passwords',
  'smtp' // repeated
]
arr.filter(function(val, idx, self){ return self.indexOf(val) === idx })

returns

[ 'google',
  'mail',
  'smtp',
  'exim4',
  'ubuntu',
  'authentication',
  'passwords' ]