Baseline Widely available *
Set
ç©ä»¶å¯è®ä½ å²åä»»ä½é¡åçå¯ä¸å¼ï¼uniqueï¼ï¼ä¸è«æ¯åºæ¬åå¥ï¼primitiveï¼å¼æç©ä»¶åèï¼referencesï¼ã
const set1 = new Set([1, 2, 3, 4, 5]);
console.log(set1.has(1));
// Expected output: true
console.log(set1.has(5));
// Expected output: true
console.log(set1.has(6));
// Expected output: false
èªæ³ 忏
iterable
è¥ä¸åå¯è¿ä»£ç©ä»¶è¢«å³å
¥ï¼å
¶ææçå
ç´ å°æè¢«å å
¥è³æ°ç Set
ãè¥ä½ æ²ææå®æ¤åæ¸ï¼æåæ¸å¼çº null
ï¼åæ°ç Set
ææ¯ç©ºçã
ä¸åæ°ç Set
ç©ä»¶ã
Set
å°è±¡æ¯æ¸å¼çæ¶éå¨ãä½ å¯ä»¥ææå
¥é åºè¿ä»£æ¶éå¨ä¸çå
ç´ ãå¨ Set
裡çå
ç´ åªæåºç¾ä¸æ¬¡ï¼æå³å¨ Set
裡çå
ç´ é½æ¯ç¨ä¸ç¡äº
å çºå¨ Set 裡æ¯åå¼é½æ¯ç¨ç«çï¼æä»¥é½ææª¢æ¥å¼çç¸çæ§ã卿©æç ECMAScript è¦ç¯çæ¬ä¸ï¼æ¤èç®æ³è·åºæ¼===æä½ç¬¦ä¸ä½¿ç¨çç®æ³ä¸¦ä¸ç¸åãå
·é«ä¾èªªï¼å¨ Set
裡 +0ï¼å¨å´æ ¼æ¨¡å¼æ¯å -0 ç¸çï¼å -0 æ¯ä¸åçå¼ãç¶èå¨ ECMAScript 2015 è¦ç¯ä¸éé»å·²è¢«æ´æ¹ãè«åé±ç覽å¨ç¸å®¹æ§ä¸çãValue equality for -0 and 0ãã
å¦å¤ï¼NaN å undefined é½å¯ä»¥è¢«æ¾ç½®å¨ Set ä¸ï¼ NaN ä¹é被è¦çºç¸åçå¼ï¼å管 NaN !== NaNï¼ã
Set.length
The value of the length
property is 0.
Set[Symbol.species]
The constructor function that is used to create derived objects.
Set.prototype
Represents the prototype for the Set
constructor. Allows the addition of properties to all Set
objects.
Set
ç©ä»¶å¯¦é«
All Set
instances inherit from Set.prototype
.
Set.prototype[Symbol.toStringTag]
The initial value of the Symbol.toStringTag
property is the string "Set"
. This property is used in Object.prototype.toString()
.
Set.prototype.size
Returns the number of values in the Set
object.
Set.prototype.add()
Inserts a new element with a specified value in to a Set
object, if there isn't an element with the same value already in the Set
.
Set.prototype.clear()
Removes all elements from the Set
object.
Set.prototype.delete()
Removes the element associated to the value
and returns a boolean asserting whether an element was successfully removed or not. Set.prototype.has(value)
will return false
afterwards.
Set.prototype.has()
Returns a boolean asserting whether an element is present with the given value in the Set
object or not.
Set.prototype[Symbol.iterator]()
Returns a new iterator object that yields the values for each element in the Set
object in insertion order.
Set.prototype.values()
Returns a new iterator object that yields the values for each element in the Set
object in insertion order.
Set.prototype.keys()
An alias for Set.prototype.values()
.
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 similar to the Map
object, so that each entry's key is the same as its value for a Set
.
Set.prototype.forEach()
Calls callbackFn
once for each value present in the Set
object, in insertion order. If a thisArg
parameter is provided, it will be used as the this
value for each invocation of callbackFn
.
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}, {"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}, {"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}, {"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}, {"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}, {"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
實ä½åºæ¬ç set æä½
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 = "India";
var mySet = new Set(text); // Set ['I', 'n', 'd', 'i', 'a']
mySet.size; // 5
è¦ç¯ ç覽å¨ç¸å®¹æ§ åè¦
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4