Меню
×
   ❮     
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 Map() Method

You can create a map by passing an array to the new Map() constructor:

Example

// Create a Map
const fruits = new Map([
  ["apples", 500],
  ["bananas", 300],
  ["oranges", 200]
]);
Try it Yourself »

Map.get()

You get the value of a key in a map with the get() method

Example

fruits.get("apples");
Try it Yourself »

Map.set()

You can add elements to a map with the set() method:

Example

// Create a Map
const fruits = new Map();

// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);
Try it Yourself »

The set() method can also be used to change existing map values:

Example

fruits.set("apples", 500);
Try it Yourself »

Map.size

The size property returns the number of elements in a map:

Example

fruits.size;
Try it Yourself »

Map.delete()

The delete() method removes a map element:

Example

fruits.delete("apples");
Try it Yourself »

Map.clear()

The clear() method removes all the elements from a map:

Example

fruits.clear();
Try it Yourself »

Map.has()

The has() method returns true if a key exists in a map:

Example

fruits.has("apples");
Try it Yourself »

Try This:

fruits.delete("apples");
fruits.has("apples");
Try it Yourself »


Map.forEach()

The forEach() method invokes a callback for each key/value pair in a map:

Example

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

Map.entries()

The entries() method returns an iterator object with the [key,values] in a map:

Example

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

Map.keys()

The keys() method returns an iterator object with the keys in a map:

Example

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

Map.values()

The values() method returns an iterator object with the values in a map:

Example

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

You can use the values() method to sum the values in a map:

Example

// Sum all values
let total = 0;
for (const x of fruits.values()) {
  total += x;
}
Try it Yourself »

Objects as Keys

Being able to use objects as keys is an important Map feature.

Example

// Create Objects
const apples = {name: 'Apples'};
const bananas = {name: 'Bananas'};
const oranges = {name: 'Oranges'};

// Create a Map
const fruits = new Map();

// Add new Elements to the Map
fruits.set(apples, 500);
fruits.set(bananas, 300);
fruits.set(oranges, 200);
Try it Yourself »

Remember: The key is an object (apples), not a string ("apples"):

Example

fruits.get("apples");  // Returns undefined
Try it Yourself »

Complete Map Reference

For a complete Map reference, go to our:

Complete JavaScript Map 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.