Skip to content

Instantly share code, notes, and snippets.

@gr0uch
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save gr0uch/e7f7146735d7aec2f795 to your computer and use it in GitHub Desktop.

Select an option

Save gr0uch/e7f7146735d7aec2f795 to your computer and use it in GitHub Desktop.
Get the union of arrays by means of the Set type.
/**
* Get the union of arrays with unique values by means of the Set type.
*
* @param {Array}
* @return {Array}
*/
function union () {
return [...new Set(Array.prototype.reduce.call(arguments,
(memo, array) => {
memo.push(...array)
return memo
}, []))]
}
console.log(union([1, 2, 3], [2, 3, 4])) // => [1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment