JavaScript: Set

JavaScript: Set

The Set data type is a collection of unique values. It allows you to store values of any data type, such as numbers, strings, booleans, objects, or even other Sets, but each value can only appear once within the Set. The order of the elements in a Set is maintained as they are inserted, and you can iterate over the elements in the same order.

Creating a Set To create a Set in JavaScript, you can use the Set() constructor or the new Set() syntax:

const mySet = new Set();
const mySet2 = new Set([1, 'hello', { key: 'value' }]);        

In the first example, an empty Set is created. In the second example, a Set is created with three elements: a number, a string, and an object.

Adding and Removing Elements To add an element to a Set, you can use the add() method:

mySet.add(1);
mySet.add('hello');
mySet.add({ key: 'value' });        

You can also add multiple elements at once using the spread operator:

mySet.add(...[1, 'hello', { key: 'value' }]);        

To remove an element from a Set, you can use the delete() method:

mySet.delete('hello');        

Checking if an Element Exists You can check if an element exists in a Set using the has() method:

mySet.has(1); // true
mySet.has('hello'); // false        

Getting the Size of a Set You can get the size of a Set using the size property:

mySet.size; // 2        

Iterating over a Set You can iterate over a Set using a for...of loop or the forEach() method:

for (const element of mySet) {
  console.log(element);
}

mySet.forEach((element) => {
  console.log(element);
});        

Set Operations Sets have several built-in operations that allow you to perform common set operations, such as union, intersection, and difference.

Union

The union of two Sets contains all the unique elements from both Sets:

const setA = new Set([1, 2, 3]);
const setB = new Set([2, 3, 4]);

const union = new Set([...setA, ...setB]);        

Intersection

The intersection of two Sets contains only the elements that are present in both Sets:

const setA = new Set([1, 2, 3]);
const setB = new Set([2, 3, 4]);

const intersection = new Set([...setA].filter(x => setB.has(x)));        

Difference

The difference of two Sets contains only the elements that are present in the first Set but not in the second Set:

const setA = new Set([1, 2, 3]);
const setB = new Set([2, 3, 4]);

const difference = new Set([...setA].filter(x => !setB.has(x)));        

Conclusion

In conclusion, the Set data type in JavaScript is a powerful tool for managing collections of unique values. With Sets, you can add and remove elements, check if an element exists, iterate over the elements, and perform set operations such as union, intersection, and difference. Understanding Sets can help you write more efficient and concise code in your JavaScript applications.

Hello Bharath... We post 100's of job opportunities for developers daily here. Candidates can talk to HRs directly. Feel free to share it with your network. Visit this link - https://jobs.hulkhire.com And start applying.. Will be happy to address your concerns, if any

回复

要查看或添加评论,请登录

Bharath Kumar Murugan的更多文章

  • Functional Programming in JavaScript

    Functional Programming in JavaScript

    Functional Programming is a programming paradigm that emphasizes the use of pure functions to solve problems. It is a…

  • JavaScript: Mutable & Immutable

    JavaScript: Mutable & Immutable

    In JavaScript, objects can be mutable or immutable. A mutable object can be changed after it is created, while an…

  • JavaScript: Public, Private and Protected

    JavaScript: Public, Private and Protected

    JavaScript is an object-oriented programming language that allows developers to create objects with properties and…

  • JavaScript Prototype: Understanding Object Inheritance

    JavaScript Prototype: Understanding Object Inheritance

    When writing JavaScript code, you may come across the term "prototype". It's an important concept to understand, as it…

    1 条评论
  • JavaScript Dynamic Import

    JavaScript Dynamic Import

    If you're a JavaScript developer, you're probably familiar with the import statement that is used to load modules…

  • JavaScript: Iterators & Generators

    JavaScript: Iterators & Generators

    Iterators In JavaScript, an iterator is an object that provides a sequence of values, one at a time, when requested…

    1 条评论
  • JavaScript Currying

    JavaScript Currying

    JavaScript currying is a technique used to transform a function that takes multiple arguments into a sequence of…

    1 条评论
  • Space and Time Complexities

    Space and Time Complexities

    As a programmer, it's essential to understand the concepts of space and time complexity in your code. These concepts…

  • JavaScript best practices

    JavaScript best practices

    As JavaScript has become one of the most widely used programming languages, it is crucial to follow the best practices…

  • JavaScript Design Patterns: The Secret to Writing Maintainable Code

    JavaScript Design Patterns: The Secret to Writing Maintainable Code

    As a JavaScript developer, you know that writing clean, efficient, and maintainable code is essential. But with the…

社区洞察

其他会员也浏览了