본문 바로가기
Web/JavaScript

[JavaScript] Set Object

by llHoYall 2022. 7. 13.

The Set object lets you store unique values of any type, whether primitive values or object references.

Create an Set Object

The Set constructor creates a Set object.

const set = new Set(); // Set(0) {}
const set = new Set([1, 2, 3, 3]); // Set(3) {1, 2, 3}
const set = new Set('Hello'); // Set(4) {'H', 'e', 'l', 'o'}

Set objects consider that all NaNs are the same.

console.log(NaN === NaN); // false
console.log(+0 === -0); // true

const set = new Set([NaN, NaN]); // Set(1) {NaN}
const set = new Set([+0, -0]); // Set(1) {0}

Set Instance

size

This property returns the number of values in the Set object.

It only has a getter.

const { size } = new Set([1, 2, 3, 3]); // 3

Set Method

add()

This method adds value to the Set object and returns the Set object with added value.

const set = new Set();

set.add(1).add(2); // Set(2) {1, 2}

clear()

This method removes all elements from the Set object.

const set = new Set([1, 2, 3]);

set.clear(); // Set(0) {}

delete(value)

This method removes the element associated with the value and returns a boolean asserting whether an element was successfully removed or not.

const set = new Set([1, 2, 3]);

set.delete(0); // false
set.delete(2); // true
// Set(2) {1, 3}

has(value)

This method returns a boolean asserting whether an element is present with the given value in the Set object or not.

const set = new Set([1, 2, 3]);

set.has(2); // true
set.has(4); // false

values() and keys()

The values() method returns a new iterator object that yields the values for each element in the Set object in insertion order.

The keys() method is an alias for values() method.

const set = new Set([1, 2, 3]);

set.values(); // SetIterator
set.keys(); // SetIterator

entries()

This method returns a new iterator object that contains an array of [value, value] for each element in the Set object, in insertion order.

const set = new Set([1, 2, 3]);

set.entries(); // SetIterator

forEach()

This method calls the callback function once for each value present in the Set object, in insertion order.

const set = new Set([1, 2, 3]);

set.forEach((e) => console.log(e));

We can traverse the Set object in other ways.

// for ... of
for (const e of set) {
  console.log(e);
}

// Spread Operator
console.log([...set]);

Mathematical Set Operation

Intersection

Set.prototype.intersection = function (set) {
  return new Set([...this].filter(e => set.has(e)));
};

Union

Set.prototype.union = function (set) {
  return new Set([...this, ...set]);
};

Difference

Set.prototype.difference = function (set) {
  return new Set([...this].filter(e => !set.has(e)));
};

Is Superset

Set.prototype.isSuperset = function (subset) {
  const supersetArr = [...this];
  return [...subset].every(e => supersetArr.includes(e));
};

'Web > JavaScript' 카테고리의 다른 글

[Svelte] Getting Started with SvelteKit  (0) 2022.12.01
[JavaScript] Map Object  (0) 2022.07.14
[JavaScript] String Object  (0) 2022.07.10
[JavaScript] RegExp Object  (0) 2022.07.09
[JavaScript] Date Object  (0) 2022.07.09

댓글