[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Set [Back]  [Original]

Set - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Set

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

* Some parts of this feature may have varying levels of support.

Set .

In this article

Set . Set , Set . Set . add() Set (, add() Set ) .

Set " " . (O(1) ), (O(log(N)) ) O(N) .

. ( 0 -0 . .) , === NaN NaN (NaN !== NaN ) . () . , . Set .

has Set Set . length Set size Array.prototype.includes .

Set

Set . .

A.difference(B) Set ABA\setminus B      . A B  A B   . [ . A B A B .]
A.intersection(B) Set ABA\cap B      . A B     . [ . A B .]
A.symmetricDifference(B) Set (AB)(BA)(A\setminus B)\cup(B\setminus A)      . A B              . [ . A B .]
A.union(B) Set ABA\cup B      . A B           . [ . A B .]
A.isDisjointFrom(B) Boolean AB=A\cap B = \empty      .      A B  . [ . A B .]
A.isSubsetOf(B) Boolean ABA\subseteq B      . A B   A B  . [ . A B A B .]
A.isSupersetOf(B) Boolean ABA\supseteq B      . B A     A B  . [ . B A A B .]

Set Set .

Set (Set-like objects)

Set this Set , Set . Set .

Map size, has(), keys() Set Set .

js
const a = new Set([1, 2, 3]);
const b = new Map([
  [1, "one"],
  [2, "two"],
  [4, "four"],
]);
console.log(a.union(b)); // Set(4) {1, 2, 3, 4}

: Set [Symbol.iterator]() keys() . entries has() keys Set .

has() size keys() Set . WeakSet keys() Set .

Set API

Set ( " Set ") Set API .

Set . Set Set . Set .

IDL . GPUSupportedFeatures / Set . IDL .

webidl
interface GPUSupportedFeatures {
  readonly setlike<DOMString>;
};

Set - ( IDL readonly ).

Set .

Set .

Set .

Set()

Set .

Set[Symbol.species]

.

Set.prototype Set .

Set.prototype.constructor

. Set Set .

Set.prototype.size

Set .

Set.prototype[Symbol.toStringTag]

[Symbol.toStringTag] "Set" . Object.prototype.toString() .

Set.prototype.add()

Set Set .

Set.prototype.clear()

Set .

Set.prototype.delete()

value . Set.prototype.has(value) false .

Set.prototype.difference()

Set Set Set Set .

Set.prototype.entries()

Set [value, value] . Map Set .

Set.prototype.forEach()

Set callbackFn . thisArg , callbackFn this .

Set.prototype.has()

Set .

Set.prototype.intersection()

Set Set Set Set .

Set.prototype.isDisjointFrom()

Set Set Set .

Set.prototype.isSubsetOf()

Set Set Set .

Set.prototype.isSupersetOf()

Set Set Set .

Set.prototype.keys()

Set.prototype.values() .

Set.prototype.symmetricDifference()

Set Set Set Set Set Set .

Set.prototype.union()

Set Set Set Set .

Set.prototype.values()

Set yield .

[Symbol.iterator]()

Set yield .

Set

js
const mySet1 = new Set();

mySet1.add(1); // Set(1) { 1 }
mySet1.add(5); // Set(2) { 1, 5 }
mySet1.add(5); // Set(2) { 1, 5 }
mySet1.add("some text"); // Set(3) { 1, 5, 'some text' }
const o = { a: 1, b: 2 };
mySet1.add(o);

mySet1.add({ a: 1, b: 2 }); // o        

mySet1.has(1); // true
mySet1.has(3); // false, 3  set   .
mySet1.has(5); // true
mySet1.has(Math.sqrt(25)); // true
mySet1.has("Some Text".toLowerCase()); // true
mySet1.has(o); // true

mySet1.size; // 5

mySet1.delete(5); // set 5 
mySet1.has(5); // false, 5  .

mySet1.size; // 4,    

mySet1.add(5); // Set(5) { 1, 'some text', {...}, {...}, 5 } -      ,      .

console.log(mySet1); // Set(5) { 1, "some text", {}, {}, 5 }

set

set .

js
for (const item of mySet1) {
  console.log(item);
}
// 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5

for (const item of mySet1.keys()) {
  console.log(item);
}
// 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5

for (const item of mySet1.values()) {
  console.log(item);
}
// 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5

//    .
for (const [key, value] of mySet1.entries()) {
  console.log(key);
}
// 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5

// Array.from  Set    
const myArr = Array.from(mySet1); // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}, 5]

//   HTML    .
mySet1.add(document.body);
mySet1.has(document.querySelector("body")); // true

// Set  
const mySet2 = new Set([1, 2, 3, 4]);
console.log(mySet2.size); // 4
console.log([...mySet2]); // [1, 2, 3, 4]

// (intersection)   .
const intersection = new Set([...mySet1].filter((x) => mySet2.has(x)));

// (difference)   .
const difference = new Set([...mySet1].filter((x) => !mySet2.has(x)));

// Set   forEach()  
mySet2.forEach((value) => {
  console.log(value);
});
// 1
// 2
// 3
// 4

Set

js
function isSuperset(set, subset) {
  for (const elem of subset) {
    if (!set.has(elem)) {
      return false;
    }
  }
  return true;
}

function union(setA, setB) {
  const _union = new Set(setA);
  for (const elem of setB) {
    _union.add(elem);
  }
  return _union;
}

function intersection(setA, setB) {
  const _intersection = new Set();
  for (const elem of setB) {
    if (setA.has(elem)) {
      _intersection.add(elem);
    }
  }
  return _intersection;
}

function symmetricDifference(setA, setB) {
  const _difference = new Set(setA);
  for (const elem of setB) {
    if (_difference.has(elem)) {
      _difference.delete(elem);
    } else {
      _difference.add(elem);
    }
  }
  return _difference;
}

function difference(setA, setB) {
  const _difference = new Set(setA);
  for (const elem of setB) {
    _difference.delete(elem);
  }
  return _difference;
}

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

isSuperset(setA, setB); // true 
union(setA, setC); // Set {1, 2, 3, 4, 5, 6} 
intersection(setA, setC); // Set {3, 4} 
symmetricDifference(setA, setC); // Set {1, 2, 5, 6} 
difference(setA, setC); // Set {1, 2} 

js
const myArray = ["value1", "value2", "value3"];

// Set    Set 
const mySet = new Set(myArray);

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

//    set  
console.log([...mySet]); // Will show you exactly the same Array as myArray

js
//      
const numbers = [2, 13, 4, 4, 2, 13, 13, 4, 4, 5, 5, 6, 6, 7, 5, 32, 13, 4, 5];

console.log([...new Set(numbers)]); // [2, 13, 4, 5, 6, 7, 32]

js
//   (set "F" "f"   )
new Set("Firefox"); // Set(7) [ "F", "i", "r", "e", "f", "o", "x" ]

//    ("f"  2 , set   )
new Set("firefox"); // Set(6) [ "f", "i", "r", "e", "o", "x" ]

Set

js
const array = Array.from(document.querySelectorAll("[id]")).map((e) => e.id);

const set = new Set(array);
console.assert(set.size === array.length);

Specification
ECMAScript 2027 LanguageSpecification
# sec-set-objects


Web Proxy Viewer  |  New URL  |  Original Page