AppDividend
Latest Code Tutorials

Getting Started With Sets In Javascript

382

Get real time updates directly on you device, subscribe now.

Getting Started With Sets In Javascript is today’s leading topic. The Set object lets you store the unique values of any type, whether there are primitive values or object references. The primary condition of the values should be in one Set is that value in a Set has to be the unique, the value equality will be checked. Sets are the new object type with the ES6 (ES2015) that allows creating collections of unique values. The values in the Set can be either simple primitives like strings or integers, but more complex nonprimitive types like object literals or arrays can also be part of a Set.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Getting Started With Sets In Javascript

The syntax of Set is following.

new Set([iterable]);

If an iterable object is passed then all of its elements will be added to a new Set. If you don’t specify this parameter or fail to specify the parameter, or its value is null, then the new Set is empty.

#Add the values in the Set

Let us take an example of Sets in Node.js.

// server.js

const ASet = new Set();

ASet.add('Cream');

console.log(ASet);

 

Getting Started With Sets In Javascript

Also, we can add the object inside the Set.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});

console.log(ASet);

Now, check the values in the Set using the following code.

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});

console.log(ASet.has('Cream')); // true

So, you can call the has method on Set to find whether the value is true or false.

#Iterating Sets

We can iterate the sets using the for-of loop. Let us take the for-of loop example.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('12');

for(set of ASet)
  console.log(set);

We can also iterate the keys.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('12');

for(set of ASet.keys())
  console.log(set);

Now iterate the values.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('12');

for(set of ASet.values())
  console.log(set);

#Iterate the Set values in the forEach

We can iterate the Set values in the forEach function using the following code.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('12');

ASet.forEach(value => console.log(value));
Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Start your free trial

#Clear the Set values

We can clear the Set values using the following command.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.clear();
console.log(ASet);

 

Sets In JavaScript

#Check the length of Set

We can check the length of a Set using the size method.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});

console.log(ASet.size);

#Remove the value from Set

We can delete the particular value from Set using the delete() method.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('2');

console.log(ASet);

ASet.delete('2');

console.log(ASet);

 

Related Posts
1 of 13

JavaScript Sets Example Tutorial

So, we have passed two as an argument to remove that item from the Set.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.