Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript spread array without duplicates

// How to ES6 spread arrays without duplicates using Set
// Because each value in the Set has to be unique, the value equality will be checked.
let eventNamesArray = ['onMutation', 'onError']
eventNamesArray = [...new Set([...eventNamesArray, 'onReady', 'onError'])]
// ['onMutation', 'onReady', 'onError']

// if you don't specifically need an array, ie you're going to forEach
// the result, then can work with Set object
const setObj = new Set([...eventNamesArray, 'onReady', 'onError'])
setObj.forEach()
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #javascript #spread #array #duplicates
ADD COMMENT
Topic
Name
3+6 =