Hi!
The most straightforward method for that is the JavaScript Set method.
Remember that maths subject about Venn diagram?
In it, the set cannot have duplicates.
For example, the word "BOB".
The set from the word will be {B, O}, and not {B, O, B}.
Similar concept in JavaScript Set.
This Set direct filtering works only for string and number items from an array.
Input Array
For example, we have these arrays:
Filtering Duplicates Using Set Method
The new Array from that will consist of unique items.
Helper Function
Let's put that method as a function:
Or, using arrow function syntax:
Usage
Object (or Array) Item Type
We can use JSON.stringify to convert each item into a valid JSON string. Afterwards, use JSON.parse to revert each back.
Let's try that:
Please be careful if the object is MASSIVE when using the method or function above.
And keep in mind, JSON.stringify for this kind of input —
— will completely omit theKey.
And of course, if there's Function in the array, it will also be trimmed by the JSON.stringify.
👋