Is there a map object in javascript?

WBOY
Release: 2022-07-01 16:00:06
Original
2138 people have browsed it

There is a map object in JavaScript; the map object saves key-value pairs and is a collection of key-value pairs. When the map object stores key-value pairs, the keys can be of any data type. The map object remembers the original value of the key. Insertion order and having a property indicating the size of the map, being able to use objects as keys is an important feature of Maps.

Is there a map object in javascript?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

There is a map object in javascript

The Map object stores key-value pairs, and the keys can be of any data type.

Map objects remember the original insertion order of keys.

Map objects have properties that represent the size of the map.

Map targets Object, which is a key-value pair data structure similar to an object.

  • Different from Object, Map’s keys can be any type of value. (null will be treated as undefined) , not just strings
  • The key values ​​of Map are ordered, and the length can be obtained through size
  • Map frequently adds or deletes key-value pairs There will be some performance advantages in the scenario
let map = new Map([
    ["姓名", "张三"],
    ["年龄","18"],
    [null, null],
    [undefined, null]])// 类似于:let obj = {
    '姓名': '张三',
    '年龄': 18,
    'null': null,
    'undefined': null}map.set(obj, 'new obj')console.log(...map)
Copy after login

Is there a map object in javascript?

Operation method

Method/property Description
size property, obtains the length of the current Map object, the same as the length of the array
set(key,value) Add a value to the current Map object, and the returned Map object supports chain writing
get(key) Find value through key (When key is an object, it must be a reference to the same object), if not found, return undefined
delete (key) Delete a value in the current Map object and return a Boolean value indicating whether the deletion is successful
has(key) Detection Whether this value is an element of the current Map object is represented by the returned Boolean value
clear() Clears all elements of the current Map object, no return value

Map traversal method

Method/property Function introduction
keys() Returns the traverser for the key name of the Set object
values() Return The traverser for the key value of the Set object
entries() Returns the traverser for the key value pair of the Set object
forEach() Use the callback function to traverse each element of the Set object. You can accept the second parameter, which is used to bind this
for(let item of map.entries()) {
    console.log(item[0], item[1])}// 等同于for(let [key, value] of map) {
    console.log(key, value)}map.forEach((value, key, map)=> {
    console.log(key, value)})
Copy after login

Is there a map object in javascript?

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of Is there a map object in javascript?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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