Loading...
const unique = new Set(tags);
save(JSON.stringify(unique));const unique = [...new Set(tags)];
save(JSON.stringify(unique));A Set serializes to '{}', so spread it into an array first to keep the values.
new Set([{ id: 1 }, { id: 1 }]) keeps both objects.set.length and getting undefined.new Set(), which compares by reference.set.size, not set.length.JSON.stringify(), which turns a Set into '{}'.Set compare values?It uses SameValueZero: like ===, but NaN equals NaN. Objects are equal only when they are the same reference.
Set faster than an array for lookups?Usually yes for large collections. set.has() does not scan every element, while array.includes() does.
Set keep insertion order?Yes. Iteration follows the order in which values were first added.