const mergeArrayUnique = (a, b) => [...new Set([...a, ...b])];
Returns a new Array which contains all the values of the two Arrays. Ensures that the new array contains only unique values. Works only for primitive values (string, number, bigint, boolean, undefined, symbol, and null).
The repository & npm package
You can find the all the utility functions from this series at github.com/martinkr/onelinecode
The library is also published to npm as @onelinecode for your convenience.
The code and the npm package will be updated every time I publish a new article.
Follow me on Twitter: @martinkr and consider to buy me a coffee
Photo by zoo_monkey on Unsplash
Top comments (3)
The code is very efficient but I would not use it in a professional environment without a comment explaining what it does. Less experienced team members may well look at that and not understand what is doing.
It's not efficient if the developer that comes after you takes twenty minutes to figure out what the heck is going on.
This will only work with primitives. With more complicated cases, like array of objects, it will not work
thank you for your contribution, yes exactly, it's leveraging the set object. It's not doing a deep comparison.
I will update the description to be more precise.