ES6 Set allows you to duplicate an array / object

Please see below script. I am testing it using Chrome.

/*declare a new set*/ var items = new Set() /*add an array by declaring as array type*/ var arr = [1,2,3,4]; items.add(arr); /*print items*/ console.log(items); // Set {[1, 2, 3, 4]} /*add an array directly as argument*/ items.add([5,6,7,8]); /*print items*/ console.log(items); // Set {[1, 2, 3, 4], [5, 6, 7, 8]} /*print type of items stored in Set*/ for (let item of items) console.log(typeof item); //object, object /*check if item has array we declared as array type*/ console.log(items.has(arr)); // true /*Now, check if item has array we added through arguments*/ console.log(items.has([5,6,7,8])); //false /*Now, add same array again via argument*/ items.add([1,2,3,4]); /*Set has duplicate items*/ console.log(items); // Set {[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4]} 
  • Why does it return false in items.has([5,6,7,8]) ?
  • Why does this allow duplicate values? I thought: "The set is in an ordered list of values ​​that cannot contain duplicates"
  • How to access the array added by items.add([5,6,7,8]) ?
+5
source share
3 answers
  • Why does it return false in items.has([5,6,7,8]) ?

    From MDN

    The Set object allows you to store unique values ​​of any type, whether it is primitive values ​​or object references .

    Objects are compared using a reference rather than a value. To compare values, the SameValueZero(x, y) comparison method is used. It says Return true if x and y are the same value of the object . Otherwise, return false .

  • Why does this allow duplicate values? I thought: "The set is in an ordered list of values ​​that cannot contain duplicates"

    Same as # 1. They say that a non-primitive value already exists in the set if the same object (and not the same view) is already added to the set.

  • How to access the array added by items.add([5,6,7,8]) ?

    You must create a variable and add the variable to the set. Then this variable can be used to check whether this array has this array or not.

+7
source

Quote Specification :

Set objects are collections of ECMAScript language values. A different value can appear only once as an element of the Sets collection. Distinctive values ​​are distinguished using the SameValueZero comparison algorithm.

(my emphasis)

SameValueZero comparison algorithm handles any two arguments of the same type (where this type is not null , undefined , string, number, boolean or character) as follows:

Returns true if x and y are the same object value. Otherwise, return false .

Ultimately, this means that the objects are compared by reference (that is, effectively, "do these two objects point to the same place in memory?") Each time you create an array using either brackets ( [] ) or a constructor call ( new Array() ), you create a new Array instance in memory. When you store a reference to an array, it will match (e.g. arr === arr ). When you create new instances and compare them, they will not match, although their contents will be compared equally (for example, [1, 2, 3] !== [1, 2, 3] ).

+2
source
  • Comparing arrays does not compare values, compares references, therefore returns false. [1] === [1] always returns false.
  • See link MDN

The Set object allows you to store unique values ​​of any type, whether it is primitive values ​​or object references.

you are passing a new object without a link so that it can add duplicates. which is actually visually similar, but the links are different.

  1. Pass the link by assigning it to a variable to access the added array.
+1
source

Source: https://habr.com/ru/post/1246998/


All Articles