2

Consider:

const ages = [26, 27, 26, 26, 28, 28, 29, 29, 30]
const uniqueAges = [...new Set(ages)]

console.log(uniqueAges) // [26,27,28,29,30]

I know the spread syntax. I searched about set, and I didn't find an explanation about how it works.

How can I set comparison of items and find similar items?

If set, use a loop in the comparison.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hassan Al Najjar
  • 324
  • 1
  • 4
  • 13
  • 3
    one mathematical property of a set is that is contains unique values. You convert the array to a set and spread the result back into an array. This is an idiomatic way to get an array with unique values – The Fool Sep 16 '20 at 21:53
  • 1
    Does this answer your question? [es6 unique array of objects with set](https://stackoverflow.com/questions/39997067/es6-unique-array-of-objects-with-set) – Clément Baconnier Sep 16 '20 at 21:53
  • 3
    See [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) for information on `Set`s. Essentially a Set is a collection of unique values - ie it can't contain duplicates. So `new Set(ages)` is a Set containing all the values in `ages`, which duplicates necessarily removed. Then the spread operator just converts this back into an array - the overall effect being to just remove duplicates from the array. – Robin Zigmond Sep 16 '20 at 21:54
  • so this use a loop in this operation ? and thank you guys – Hassan Al Najjar Sep 16 '20 at 21:59
  • Related: *[What is the 'new' keyword in JavaScript?](https://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript)*. – Peter Mortensen Nov 29 '22 at 11:15

1 Answers1

6

Set can only contain unique values; here is what constitutes an equal value according to MDN:

Because each value in the Set has to be unique, the value equality will be checked. In an earlier version of ECMAScript specification, this was not based on the same algorithm as the one used in the === operator. Specifically, for Sets, +0 (which is strictly equal to -0) and -0 were different values. However, this was changed in the ECMAScript 2015 specification. See "Key equality for -0 and 0" in the browser compatibility table for details.

NaN and undefined can also be stored in a Set. All NaN values are equated (i.e. NaN is considered the same as NaN, even though NaN !== NaN).

There is an implicit loop in creation of the Set, using the iterable interface of the passed array. There is also a loop in ..., which unpacks an iterable.

However, checking whether an element is already in Set is not done using a loop - for all intents and purposes you can think of a Set as a dictionary with only keys, and no values. The code is almost the same as:

const ages = [26, 27, 26, 26, 28, 28, 29, 29, 30]

const temp = {};
for (const age of ages) {
  temp[age] = true;
}

const uniqueAges = [];
for (const age in temp) {
  uniqueAges.push(age);
}

console.log(uniqueAges);

I am intentionally writing this "low tech" way to illustrate what is going on; notice that object keys are always strings so the result will be stringified. Using a Map or also storing ages as values would make it much more similar in effect to OP's code, but I wanted to keep it simple for the sake of illustration.

Amadan
  • 191,408
  • 23
  • 240
  • 301