This is a new technology, part of the ECMAScript 2015 (ES6) standard .
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.
L'objecte Set permet emmagatzemar valors únics de qualsevol tipus, ja siguin valors primitius o bé referències a objectes.
Sintaxi
new Set([iterable]);
Paràmetres
- iterable
- Si rep un objecte iterable, tots els seus elements seran afegits al nou Set. null serà tractat com a undefined.
Descripció
Els objectes Set són coleccions de valors, els seus elements poden ser iterats en ordre d'inserció. Un valor només pot aparèixer un cop dins el Set; és únic dins la col·lecció del Set.
Igualtat de valors
Degut a que cada valor dins el Set ha de ser únic, la igualtat dels valors serà comprovada i aquesta no es basa en el mateix algoritme que l'emprat per l'operador ===. Concreatement, per a Sets, +0 (que és estrictament igual a -0) i -0 són valors diferents. Tot i així, aquest comportament s'ha canviat a la última especificació de l'ECMAScript 6. A partir de Gecko 29.0 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26) (errada 952870) i una recent versió nightly del Chrome, +0 i -0 són tractats com al mateix valor en objectes Set. Així mateix, NaN i undefined també poden ser emmagatzemats dins un Set. NaN és considerat igual a NaN (tot i que NaN !== NaN).
Propietats
Set.length- El valor de la propietat
lengthés 0. get Set[@@species]- La funció constructora que s'ha utilitzat per a crear objectes derivats.
Set.prototype- Representa el prototipus per al constructor
Set. Permet afegir propietats a tots els objectesSet.
Instàncies de Set
Totes les instàncies de Set hereten de Set.prototype.
Propietats
Mètodes
Exemples
Utilitzar l'objecte Set
var mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add("algun text");
mySet.has(1); // true
mySet.has(3); // false, 3 no ha estat afegit al set
mySet.has(5); // true
mySet.has(Math.sqrt(25)); // true
mySet.has("Some Text".toLowerCase()); // true
mySet.size; // 3
mySet.delete(5); // esborra 5 del set
mySet.has(5); // false, 5 ha sigut esborrat
mySet.size; // 2, acabem d'esborrar un valor
Iterarar Sets
// iterar els elements d'un set // imprimeix els elements en l'ordre: 1, "algun text" for (let item of mySet) console.log(item); // imprimeix els elements en l'ordre: 1, "algun text" for (let item of mySet.keys()) console.log(item); // imprimeix els elements en l'ordre: 1, "algun text" for (let item of mySet.values()) console.log(item); // imprimeix els elements en l'ordre: 1, "algun text" //(key i value són iguals en aquest exemple) for (let [key, value] of mySet.entries()) console.log(key); // converteix el set en un Array (mitjançant Array comprehensions) var myArr = [v for (v of mySet)]; // [1, "algun text"] // Alternativa (mitjançant Array.from) var myArr = Array.from(mySet); // [1, "algun text"] // el codi següent també funcionarà si s'executa dins un document HTML mySet.add(document.body); mySet.has(document.querySelector("body")); // true // conversió entre Set i Array mySet2 = new Set([1,2,3,4]); mySet2.size; // 4 [...mySet2]; // [1,2,3,4] // la intersecció es pot simular via var intersection = new Set([x for (x of set1) if (set2.has(x))]); // Iteració de les entrades del set mitjançant un forEach mySet.forEach(function(value) { console.log(value); }); // 1 // 2 // 3 // 4
Relació amb objectes Array
var myArray = ["valor1", "valor2", "valor3"];
// Utilitzem el constructor normal del Set per a transformar un Array en un Set
var mySet = new Set(myArray);
mySet.has("valor1"); // retorna true
// Utilitzem l'operador spread per a transformar un Set en un Array.
console.log(uneval([...mySet])); // Mostrarà exactament el mateix Array que myArray
Especificacions
| Especificació | Estat | Comentaris |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Set' in that specification. |
Standard | Definició inicial. |
Compatibilitat amb navegadors
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suport bàsic |
38 [1] |
13 (13) | 11 | 25 | 7.1 |
Argument al constructor: new Set(iterable) |
38 | 13 (13) | Not supported | 25 | Not supported |
| iterable | 38 | 17 (17) | Not supported | 25 | 7.1 |
Set.clear() |
38 | 19 (19) | 11 | 25 | 7.1 |
Set.keys(), Set.values(), Set.entries() |
38 | 24 (24) | Not supported | 25 | 7.1 |
Set.forEach() |
38 | 25 (25) | 11 | 25 | 7.1 |
| Igualtat de valors per a -0 i 0 | 38 | 29 (29) | Not supported | 25 | Not supported |
Argument del constructor: new Set(null) |
(Yes) | 37 (37) | ? | ? | ? |
Monkey-patched add() al Constructor |
(Yes) | 37 (37) | ? | ? | ? |
Set[@@species] |
? | 41 (41) | ? | ? | ? |
Set() sense new llença excepció |
? | 42 (42) | ? | ? | ? |
| Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Suport bàsic | Not supported | 38 [1] | 13.0 (13) | Not supported | Not supported | 8 |
Argument al constructor: new Set(iterable) |
Not supported | 38 | 13.0 (13) | Not supported | Not supported | Not supported |
| iterable | Not supported | Not supported | 17.0 (17) | Not supported | Not supported | 8 |
Set.clear() |
Not supported | 38 | 19.0 (19) | Not supported | Not supported | 8 |
Set.keys(), Set.values(), Set.entries() |
Not supported | 38 | 24.0 (24) | Not supported | Not supported | 8 |
Set.forEach() |
Not supported | 38 | 25.0 (25) | Not supported | Not supported | 8 |
| Igualtat de valors per a -0 i 0 | Not supported | 38 | 29.0 (29) | Not supported | Not supported | Not supported |
Argument del constructor: new Set(null) |
? | (Yes) | 37.0 (37) | ? | ? | ? |
Monkey-patched add() al Constructor |
? | (Yes) | 37.0 (37) | ? | ? | ? |
Set[@@species] |
? | ? | 41.0 (41) | ? | ? | ? |
Set() sense new llença excepció |
? | ? | 42.0 (42) | ? | ? | ? |
[1] La característica estava disponible sota una preferència a partir de Chorem 31. Al chrome://flags, activeu l'entrada “Activa JavaScript Experimental”.