SoFunction
Updated on 2025-03-06

Details of the method of Set collection in JavaScript

Set Collection Overview

  • Set collections are very similar to Arry arrays, but Set collections store keys, that is, there cannot be two keys with equal values ​​and data types in the Set collection.
  • Set collections cannot be valued with subscripts
  • Set collection has no length attribute but size
  • Set collections can be converted into real arrays

How to create and use Set

Create a Set and add an existing variable:

// Create variablesconst a = "a";
const b = "b";
const c = "c";

// Create Setconst letters = new Set();

(a);
(b);
(c);

Add()

Adding elements to the collection will deduplicate, and adding repeated elements will only display one

var set = new Set()
(1)
(set); // {1}

var set = new Set().add(1).add([2,3,4])
(set); // {1, [2,3,4]}

var set = new Set([1,2,3,4,5,6]);
((123)); // {1, 2, 3, 4, 5, 6, 123} Instructions added successfully((1)); // {1, 2, 3, 4, 5,6,123} It means that duplicates were not added successfully

Clear()

Clear Set object

var set = new Set([1,2,3,4,5,6]);
()
(set); // {size: 0} Cleared successfully

Delete()

Delete values ​​in Set object

var set = new Set([1,2,3,4,5,6]);
((1));  // true means the deletion is successful((1));  // false indicates that the deletion failed because the collection no longer exists.(set);   // [2,3,4,5,6] 1Deleted

Entries()

The Entries() method returns an iterative object of an array that contains the key/value pairs of the array (key/value). The index value of the array in the iterative object is used as the key and the array element is used as the value.

var set = new Set([1,2,3,4,5]);
(set); // {1, 2, 3, 4, 5}
(()); // {1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5}

Has()

Determine whether a value in the Set object exists, returns true, does not exist, returns false

var set = new Set([1,2,3,4,5]);
((1)); // ture indicates that there is 1 in the Set object((6)); // false illustrateSetDoes not exist in the object6

ForEach()

Calling a callback for each element It has three parameters:

  • .value:Optional, the value of the element in the Set object.
  • .key:Optional Since the Set object has no key, the value of the key and the value of the value are the same.
  • .setObj:Optional, Set object itself.
var set = new Set([1,2,3,4,5]);
((value,key,setObj)=>{
  (value,key,setObj);
})
// 1 1 {1, 2, 3, 4, 5}
// 2 2 {1, 2, 3, 4, 5}
....

Keys() and values()

Returns the key value array of Set object, and returns the value array of Set object

var set = new Set([1,2,3,4,5]);
(()); // {1, 2, 3, 4, 5}
(()); // {1, 2, 3, 4, 5}

Size

Returns the count of elements in the Set object

var set = new Set([1,2,3,4,5]);
();  // 5

Set of methods and properties of Set objects

name illustrate
new Set() Create a new Set object.
add() Add new elements to Set.
clear() Remove all elements from Set.
delete() Deletes the element specified by its value.
entries() Returns an array of values ​​in the Set object.
has() Return true if the value exists.
forEach() Callbacks for each element.
keys() Returns an array of values ​​in the Set object.
values() Same as keys().
size Returns the element count.

This is the end of this article about the detailed methods of Set collections in JavaScript. For more related JS Set collection content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!