DEV Community

Discussion on: JavaScript: How to Remove Duplicate Values from Arrays

Collapse
 
felipemfp profile image
Felipe Pontes

Set works great when you are dealing with primitive values. But if you are working with some list of items (e.g. list of comment authors in this post), you're better off using a key mapping approach:

const mapping = {}
const unique = array.filter((item) => {
  if (item.id in mapping) return false
  mapping[item.id] = true
  return true
})