Home » Javascript17 July 2018

Javascript: Set object & Loop

A Set object holds the unique values of any type like array or object. An array or any other iterable object can be passed to Set.

– It’s a new data structure introduced in JavaScript ECMAScript 6 (ES6).
– It has useful methods for iteration over the Set values.

Get size of the Set

Check value exists in Set

Delete an element from the Set

Delete all key-value pairs of the mySet

Again adding elements to Set

Get keys and values of the Set object

– There’s no key-value pairs in Set like it is on Map.
– Hence, the keys() and values() functions return the same.
– Both return Iterator object containing the values of the Set object

Get keys of the Set

Get values of the Set

Get elements of the mySet

Print key value pairs

entries() function returns the Iterator object containing the [value, value] array for each element.

The keys and values of the Set are the same

Iterating mySet object with forEach() function

CONVERT ARRAY INTO SET & CONVERT SET INTO ARRAY

Creating Set object with multi-dimensional array as iterable

Convert Array object into Set object

Convert Set into Array

Hope this helps. Thanks.

Javascript

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed

Comments are closed.