Home > Web Front-end > JS Tutorial > body text

What is a Set in JS?

WBOY
Release: 2024-07-19 12:03:18
Original
343 people have browsed it

What is a Set in JS?

A Set is a collection of unique values. Unlike arrays, sets cannot have duplicate elements.

Basic Operations with Set

Creating a Set

You can create a new Set using the new Set() constructor. For example:

const uniqueNumbers = new Set([1, 2, 3, 3, 4]);
console.log(uniqueNumbers); // Output: Set(4) {1, 2, 3, 4}
Copy after login

Funny-Serious Examples

Example 1: The Duplicate Party Crashers
Imagine you’re having a party, and you only want unique guests. But some guests try to sneak in twice!

const partyGuests = new Set();

partyGuests.add('Alice');
partyGuests.add('Bob');
partyGuests.add('Alice'); // Alice tries to sneak in again!

console.log(partyGuests); // Output: Set(2) {"Alice", "Bob"}
Copy after login

Here, Alice tried to sneak in twice, but the Set only lets her in once.

Example 2: The Magic Hat Trick
You have a magic hat that only allows unique items to be stored. If you try to put the same item twice, it just won’t have it!

const magicHat = new Set();

magicHat.add('Rabbit');
magicHat.add('Dove');
magicHat.add('Rabbit'); // Another Rabbit tries to jump in!

console.log(magicHat); // Output: Set(2) {"Rabbit", "Dove"}
Copy after login

The magic hat only contains one rabbit and one dove, despite the second rabbit trying to jump in.

Example 3: The Library of Unique Books
You run a library where each book must be unique. If someone tries to donate a book you already have, you politely decline.

const library = new Set(['1984', 'Brave New World', '1984']); // 1984 is already in the library!

console.log(library); // Output: Set(2) {"1984", "Brave New World"}
Copy after login

Your library maintains its uniqueness by only keeping one copy of each book.

Useful Methods

Checking for a Member

Want to know if a particular guest is already at the party?

console.log(partyGuests.has('Alice')); // true
console.log(partyGuests.has('Charlie')); // false
Copy after login

Removing a Member

Someone decides to leave the party early? No problem!

partyGuests.delete('Bob');
console.log(partyGuests); // Output: Set(1) {"Alice"}
Copy after login

Clearing the Set

Party's over! Time to clear everyone out.

partyGuests.clear();
console.log(partyGuests); // Output: Set(0) {}
Copy after login

Iterating Over a Set

Just like arrays, you can iterate over sets.

const animals = new Set(['Cat', 'Dog', 'Bird']);

for (const animal of animals) {
  console.log(animal); 
  // Output:
  // Cat
  // Dog
  // Bird
}
Copy after login

Converting Between Sets and Arrays

You can convert a set to an array and vice versa.

const numberSet = new Set([1, 2, 3]);
const numberArray = [...numberSet]; // [1, 2, 3]

const arrayToSet = new Set(numberArray); // Set(3) {1, 2, 3}
Copy after login

Why Use a Set?

Sets are particularly useful when you need to store a collection of unique items, and you want to avoid duplicates without having to manually check for them.

Summary

  • Unique Values: Sets store only unique values.
  • Basic Operations: You can add, delete, and check for values in a set.
  • Iterate: You can iterate over the values in a set.
  • Conversion: Easily convert between sets and arrays.

By using sets, you ensure that your collections remain unique and efficient, whether it’s a guest list for a party, items in a magic hat, or books in a library!

The above is the detailed content of What is a Set in JS?. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!