This translation is incomplete. Please help translate this article from English.

Η δομή Set επιτρέπει την αποθήκευση μοναδικών τιμών οποιουδήποτε τύπου, είτε τιμών τύπου primitive values είτε αναφορών σε αντικείμενα.

Syntax

new Set([iterable]);

Παράμετροι

iterable
Εάν ένα iterable object περαστεί ως παράμετρος, όλα τα στοιχεία του θα προστεθούν στο καινούριοSet. Εάν δεν προσδιοριστεί αυτή η παράμετρος, ή αν η τιμή της είναι null, το καινούριο Set θα είναι άδειο.

Επιστρεφόμενη τιμή

Ένα καινούριο αντικείμενο Set.

Περιγραφή

Set αντικείμενα αποτελούν συλλογές από τιμές. Μπορεί κάποιος να κάνει περάσματα στα στοιχεία ενός set με τη σειρά που προστέθηκαν. Μια τιμή στο Set μπορεί να περιέχεται μόνο μια φορά; Είναι μοναδική στη συλλογή ενός Set.

Ισότητα τιμών

Καθώς κάθε τιμή στο Set πρέπει να είναι μοναδική, θα πρέπει να ελεγχθεί η ισότητα της τιμής. Σε μια προηγούμενη έκδοση της ECMAScript αυτό δεν ήταν βασισμένο στον ίδιο αλγόριθμο σαν αυτόν που χρησιμοποιείται στον συντελεστή ===. Συγκεκριμένα, για τα Set, το +0 (το οποίο είναι αυστηρά ίσο με το -0) και το  -0 ήταν διαφορετικές τιμές. Παρόλα αυτά, αυτό άλλαξε στον προσδιορισμό της ECMAScript 2015. Δείτε το "Value equality for -0 and 0" στον πίνακα browser compatibility για λεπτομέρειες.

Επίσης, τα NaN και undefined μπορούν επίσης να αποθηκευτούν σε ένα Set. Το NaN θεωρείται το ίδιο με το NaN (παρόλο που NaN !== NaN).

Properties

Set.length
Η τιμή της παραμέτρου length είναι 0.
get Set[@@species]
Ο κατασκευαστής που χρησιμοποιείται για να δημιουργήσει συμπληρωματικά αντικείμενα.
Set.prototype
Αναπαριστά το πρωτότυπο για τον κατασκευαστή του Set. Επιτρέπει την προσθήκη παραμέτρων σε όλα τα αντικείμενα του Set.

Set instances

Όλα τα Set instances κληρονομούν από το Set.prototype.

Properties

Set.prototype.constructor
Returns the function that created an instance's prototype. This is the Set function by default.
Set.prototype.size
Returns the number of values in the Set object.

Methods

Set.prototype.add(value)
Appends a new element with the given value to the Set object. Returns the Set object.
Set.prototype.clear()
Removes all elements from the Set object.
Set.prototype.delete(value)
Removes the element associated to the value and returns the value that Set.prototype.has(value) would have previously returned. Set.prototype.has(value) will return false afterwards.
Set.prototype.entries()
Returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in insertion order. This is kept similar to the Map object, so that each entry has the same value for its key and value here.
Set.prototype.forEach(callbackFn[, thisArg])
Calls callbackFn once for each value present in the Set object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.
Set.prototype.has(value)
Returns a boolean asserting whether an element is present with the given value in the Set object or not.
Set.prototype.keys()
Is the same function as the values() function and returns a new Iterator object that contains the values for each element in the Set object in insertion order.
Set.prototype.values()
Returns a new Iterator object that contains the values for each element in the Set object in insertion order.
Set.prototype[@@iterator]()
Returns a new Iterator object that contains the values for each element in the Set object in insertion order.

Παραδείγματα

Χρησιμοποιώντας το αντικείμενο Set

var mySet = new Set();

mySet.add(1); // Set { 1 }
mySet.add(5); // Set { 1, 5 }
mySet.add(5); // Set { 1, 5 }
mySet.add('some text'); // Set { 1, 5, 'some text' }
var o = {a: 1, b: 2};
mySet.add(o);

mySet.add({a: 1, b: 2}); // o is referencing a different object so this is okay

mySet.has(1); // true
mySet.has(3); // false, 3 has not been added to the set
mySet.has(5);              // true
mySet.has(Math.sqrt(25));  // true
mySet.has('Some Text'.toLowerCase()); // true
mySet.has(o); // true

mySet.size; // 5

mySet.delete(5); // removes 5 from the set
mySet.has(5);    // false, 5 has been removed

mySet.size; // 4, we just removed one value
console.log(mySet);// Set {1, "some text", Object {a: 1, b: 2}, Object {a: 1, b: 2}}

Κάνοντας επανάληψη στα Sets

// iterate over items in set
// logs the items in the order: 1, "some text", {"a": 1, "b": 2} 
for (let item of mySet) console.log(item);

// logs the items in the order: 1, "some text", {"a": 1, "b": 2} 
for (let item of mySet.keys()) console.log(item);
 
// logs the items in the order: 1, "some text", {"a": 1, "b": 2} 
for (let item of mySet.values()) console.log(item);

// logs the items in the order: 1, "some text", {"a": 1, "b": 2} 
//(key and value are the same here)
for (let [key, value] of mySet.entries()) console.log(key);

// convert Set object to an Array object, with Array.from
var myArr = Array.from(mySet); // [1, "some text", {"a": 1, "b": 2}]

// the following will also work if run in an HTML document
mySet.add(document.body);
mySet.has(document.querySelector('body')); // true

// converting between Set and Array
mySet2 = new Set([1, 2, 3, 4]);
mySet2.size; // 4
[...mySet2]; // [1, 2, 3, 4]

// intersect can be simulated via 
var intersection = new Set([...set1].filter(x => set2.has(x)));

// difference can be simulated via
var difference = new Set([...set1].filter(x => !set2.has(x)));

// Iterate set entries with forEach
mySet.forEach(function(value) {
  console.log(value);
});

// 1
// 2
// 3
// 4

Υλοποιώντας βασικές λειτουργίες στα sets

Set.prototype.isSuperset = function(subset) {
    for (var elem of subset) {
        if (!this.has(elem)) {
            return false;
        }
    }
    return true;
}

Set.prototype.union = function(setB) {
    var union = new Set(this);
    for (var elem of setB) {
        union.add(elem);
    }
    return union;
}

Set.prototype.intersection = function(setB) {
    var intersection = new Set();
    for (var elem of setB) {
        if (this.has(elem)) {
            intersection.add(elem);
        }
    }
    return intersection;
}

Set.prototype.difference = function(setB) {
    var difference = new Set(this);
    for (var elem of setB) {
        difference.delete(elem);
    }
    return difference;
}

//Examples
var setA = new Set([1, 2, 3, 4]),
    setB = new Set([2, 3]),
    setC = new Set([3, 4, 5, 6]);

setA.isSuperset(setB); // => true
setA.union(setC); // => Set [1, 2, 3, 4, 5, 6]
setA.intersection(setC); // => Set [3, 4]
setA.difference(setC); // => Set [1, 2]

Συσχετισμός με αντικείμενα τύπου Array

var myArray = ['value1', 'value2', 'value3'];

// Use the regular Set constructor to transform an Array into a Set
var mySet = new Set(myArray);

mySet.has('value1'); // returns true

// Use the spread operator to transform a set into an Array.
console.log([...mySet]); // Will show you exactly the same Array as myArray

Συσχετισμός με Strings

var text = 'Indiana';

var mySet = new Set(text);  // Set {'I', 'n', 'd', 'i', 'a'}
mySet.size;  // 5

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Set' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Set' in that specification.
Draft  

Συμβατότητα με Browser

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 38Edge Full support 12Firefox Full support 13IE Full support 11Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 14Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
new Set(iterable)Chrome Full support 38Edge Full support 12Firefox Full support 13IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 14Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
new Set(null)Chrome Full support YesEdge Full support 12Firefox Full support 37IE Full support 11Opera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 37Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
Set() without new throwsChrome Full support YesEdge Full support 12Firefox Full support 42IE Full support 11Opera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support 12Firefox Android Full support 42Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
Key equality for -0 and 0Chrome Full support 38Edge Full support 12Firefox Full support 29IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 29Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 4.0.0
addChrome Full support 38Edge Full support 12Firefox Full support 13IE Partial support 11
Notes
Partial support 11
Notes
Notes Returns 'undefined' instead of the 'Set' object.
Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 14Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support Yes
clearChrome Full support 38Edge Full support 12Firefox Full support 19IE Full support 11Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 19Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
deleteChrome Full support 38Edge Full support 12Firefox Full support 13IE Full support 11Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 14Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.
entriesChrome Full support 38Edge Full support 12Firefox Full support 24IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 24Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
forEachChrome Full support 38Edge Full support 12Firefox Full support 25IE Full support 11Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 25Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
hasChrome Full support 38Edge Full support 12Firefox Full support 13IE Full support 11Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 14Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support Yes
prototypeChrome Full support 38Edge Full support 12Firefox Full support 13IE Full support 11Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 14Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support Yes
sizeChrome Full support 38Edge Full support 12Firefox Full support 19
Notes
Full support 19
Notes
Notes From Firefox 13 to Firefox 18, the size property was implemented as a Set.prototype.size() method, this has been changed to a property in later versions conform to the ECMAScript 2015 specification.
IE Full support 11Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 19
Notes
Full support 19
Notes
Notes From Firefox 13 to Firefox 18, the size property was implemented as a Set.prototype.size() method, this has been changed to a property in later versions conform to the ECMAScript 2015 specification.
Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
valuesChrome Full support 38Edge Full support 12Firefox Full support 24IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 24Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
@@iteratorChrome Full support YesEdge Full support YesFirefox Full support 36
Full support 36
No support 27 — 36
Notes Alternate Name
Notes A placeholder property named @@iterator is used.
Alternate Name Uses the non-standard name: @@iterator
No support 17 — 27
Notes Alternate Name
Notes A placeholder property named iterator is used.
Alternate Name Uses the non-standard name: iterator
IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 36
Full support 36
No support 27 — 36
Notes Alternate Name
Notes A placeholder property named @@iterator is used.
Alternate Name Uses the non-standard name: @@iterator
No support 17 — 27
Notes Alternate Name
Notes A placeholder property named iterator is used.
Alternate Name Uses the non-standard name: iterator
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.12
@@speciesChrome Full support 51Edge Full support 13Firefox Full support 41IE No support NoOpera Full support 38Safari Full support 10WebView Android Full support 51Chrome Android Full support 51Edge Mobile Full support 13Firefox Android Full support 41Opera Android Full support 38Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 6.5.0
Full support 6.5.0
Full support 6.0.0
Disabled
Disabled From version 6.0.0: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.

See also

Εθελοντές και ετικέτες εγγράφου

Συνεισφέροντες σε αυτή τη σελίδα: SophieKrt
Τελευταία ενημέρωση από: SophieKrt,