Javascript Set class

Introduction

ECMAScript 6 adds a Set type.

A set is an ordered list of values without duplicates.

Create Set

Sets are created via 'new Set()' and items are added via the add() method.

You can see how many items are in a set by checking the size property:

let set = new Set();
set.add(5);//from www. j av a  2 s .c  o m
set.add("5");

console.log(set.size);    // 2

Sets do not coerce values to determine whether they are the same.

That means a set can contain both the number '5' and the string '5' as two separate items.

You can add multiple objects to the set, and those objects will remain distinct:

let set = new Set(),
    key1 = {},/*w ww  . ja  v a  2 s  .c  om*/
    key2 = {};

set.add(key1);
set.add(key2);

console.log(set.size);    // 2

Because key1 and key2 are not converted to strings, they count as two unique items in the set.

If the add() method is called more than once with the same value, all calls after the first one are ignored:

let set = new Set();
set.add(5);//w w w  .  j  a va2 s .  com
set.add("5");
set.add(5);     // duplicate - this is ignored

console.log(set.size);    // 2

We can create a set from an array, and Set constructor will keep only unique values.

For instance:

let set = new Set([1, 2, 3, 4, 5, 5, 5, 5]);
console.log(set.size);    // 5

In this example, an array with duplicate values is used to initialize the set.

The number 5 only appears once in the set.

The Set constructor uses an iterator to extract values from the argument.

You can test which values are in a set using the has() method, like this:

let set = new Set();
set.add(5);/*  www  .j  ava2  s .co  m*/
set.add("5");

console.log(set.has(5));    // true
console.log(set.has(6));    // false

Here, set.has(6) would return false because the set doesn't have that value.

Removing Values

To remove one value from a set, use the delete() method.

To remove all values from the set, use clear() method.

This code shows both in action:

let set = new Set();
set.add(5);/*from   ww  w.  j  a v a2s . co  m*/
set.add("5");

console.log(set.has(5));    // true

set.delete(5);

console.log(set.has(5));    // false
console.log(set.size);      // 1

set.clear();

console.log(set.has("5"));  // false
console.log(set.size);      // 0

Iterate

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

set.forEach(function(value, key, ownerSet) {
    console.log(key + " " + value);
    console.log(ownerSet === set);//from w  w w . j a va 2  s  .  com
});

Convert

To convert an array into a set, pass the array to the Set constructor.

To convert a set back into an array, use the spread operator.

let set = new Set([1, 2, 3, 3, 3, 4, 5]),
    array = [...set];/* w ww .  ja v  a2  s .c o  m*/

console.log(array);             // [1,2,3,4,5]

To create an array without duplicates. For example:

function eliminateDuplicates(items) {
    return [...new Set(items)];
}

let numbers = [1, 2, 3, 3, 3, 4, 5],
    noDuplicates = eliminateDuplicates(numbers);

console.log(noDuplicates);      // [1,2,3,4,5]



PreviousNext

Related