Меню
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP КАК СДЕЛАТЬ ПРОГРАММЫ SW3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS КИБЕРБЕЗОПАСНОСТЬ НАУКА О ДАННЫХ
     ❯   

JS Учебник


JS Версии


JS Объекты


JS Функции


JS Классы


JS Асинхронный


JS HTML DOM


JS Браузер BOM


JS Веб APIы


JS AJAX


JS JSON


JS или jQuery


JS Графика


JS Примеры


JS Рекомендация




JavaScript Набор методов



The new Set() Method

Pass an array to the new Set() constructor:

Example

// Create a Set
const letters = new Set(["a","b","c"]);
Try it Yourself »

The add() Method

Example

letters.add("d");
letters.add("e");
Try it Yourself »

If you add equal elements, only the first will be saved:

Example

letters.add("a");
letters.add("b");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
Try it Yourself »

Listing Set Elements

You can list all Set elements (values) with a for..of loop:

Example

// Create a Set
const letters = new Set(["a","b","c"]);

// List all Elements
let text = "";
for (const x of letters) {
  text += x;
}
Try it Yourself »

The has() Method

The has() method returns true if a specified value exists in a set.

Example

// Create a Set
const letters = new Set(["a","b","c"]);

// Does the Set contain "d"?
answer = letters.has("d");
Try it Yourself »


The forEach() Method

The forEach() method invokes a function for each Set element:

Example

// Create a Set
const letters = new Set(["a","b","c"]);

// List all entries
let text = "";
letters.forEach (function(value) {
  text += value;
})
Try it Yourself »

The values() Method

The values() method returns an Iterator object with the values in a Set:

Example 1

// Create a Set
const letters = new Set(["a","b","c"]);

// Get all Values
const myIterator = letters.values();

// List all Values
let text = "";
for (const entry of myIterator) {
  text += entry;
}
Try it Yourself »

Example 2

// Create a Set
const letters = new Set(["a","b","c"]);

// List all Values
let text = "";
for (const entry of letters.values()) {
  text += entry;
}
Try it Yourself »

The keys() Method

The keys() method returns an Iterator object with the values in a Set:

Note

A Set has no keys, so keys() returns the same as values().

This makes Sets compatible with Maps.

Example 1

// Create a Set
const letters = new Set(["a","b","c"]);

// Create an Iterator
const myIterator = letters.keys();

// List all Elements
let text = "";
for (const x of myIterator) {
  text += x;
}
Try it Yourself »

Example 2

// Create a Set
const letters = new Set(["a","b","c"]);

// List all Elements
let text = "";
for (const x of letters.keys()) {
  text += x;
}
Try it Yourself »

The entries() Method

The entries() method returns an Iterator with [value,value] pairs from a Set.

Note

The entries() method is supposed to return a [key,value] pair from an object.

A Set has no keys, so the entries() method returns [value,value].

This makes Sets compatible with Maps.

Example 1

// Create a Set
const letters = new Set(["a","b","c"]);

// Get all Entries
const myIterator = letters.entries();

// List all Entries
let text = "";
for (const entry of myIterator) {
  text += entry;
}
Try it Yourself »

Example 2

// Create a Set
const letters = new Set(["a","b","c"]);

// List all Entries
let text = "";
for (const entry of letters.entries()) {
  text += entry;
}
Try it Yourself »

Complete Set Reference

For a complete Set reference, go to our:

Complete JavaScript Set Reference.

The reference contains descriptions and examples of all Set properties and methods.



×

Связаться с отделом продаж

Если вы хотите использовать услуги schoolsw3 как образовательное учреждение, команда или предприятие, отправьте нам электронное письмо:
sales@schoolsw3.com

Сообщить об ошибке

Если вы хотите сообщить об ошибке или внести предложение, отправьте нам электронное письмо:
help@schoolsw3.com

Schoolsw3 оптимизирован для обучения и подготовки. Примеры могут быть упрощены для улучшения чтения и обучения. Учебники, ссылки и примеры постоянно проверяются, чтобы избежать ошибок, но мы не можем гарантировать полную правильность всего контента.
При использовании Schoolsw3 вы соглашаетесь прочитать и принять наши условия использования,
политику использования файлов cookie и конфиденциальности.

Авторское право 1999- принадлежит Refsnes Data. Все права защищены. Schoolsw3 работает на SW3.CSS.