Home  >  Article  >  Web Front-end  >  Parsing Map objects in JavaScript (with examples)

Parsing Map objects in JavaScript (with examples)

不言
不言forward
2018-10-18 16:47:476031browse

The content this article brings to you is about the analysis of Map objects in JavaScript (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Create a Map object

Map objects save key-value pairs. Any value (object or primitive) can be used as a key or a value

1. Constructor

Syntax: new Map([iterable])
Parameters:

iterable can be an array or other iterable object, whose elements are either key-value pairs or an array of two elements; each key-value pair will be added to a new Map, and null will be treated as undefined

let arr = [1,2,3];
let myMap = new Map(arr.entries());
console.log(myMap.get(0)); // 1

2. Map instance properties

myMap.size Accessible properties return the number of elements of the Map object

size The value of the property is an integer , indicating how many key-value pairs the Map object has. size is a read-only attribute, and its corresponding set method is undefined, that is, its value cannot be changed

let myMap = new Map();
myMap.set("a", "alpha");
myMap.set("b", "beta");
myMap.set("g", "gamma");
console.log(myMap.size); // 3

3. Map instance method

1.set ()

Syntax: myMap.set(key, value)

Parameters:
key is required, the key value of the element added to the Map object
value is required Fill in the value of the element added to the Map object

let myMap = new Map();
myMap.set("bar", "foo");
 myMap.set(1, "foobar");
// 在Map对象中更新一个新元素
myMap.set("bar", "baz");

2.get()

Syntax: myMap.get(key)

Parameters :
key The key of the element you want to get

Return value: Return the value associated with the specified key in a Map object. If the key cannot be found, undefined

let myMap = new Map();
myMap.set("bar", "foo");
console.log(myMap.get("bar"));  // "foo"
console.log(myMap.get("baz"));  // undefined

is returned 3.has()

Syntax: myMap.has(key)

Parameters:
key is required, used to detect whether there is a key value for the specified element

Return value: Returns true if the specified element exists in the Map. Return false in other cases

let myMap = new Map();
myMap.set("bar", "foo");
console.log(myMap.has("bar"));  // returns true
console.log(myMap.has("baz"));  // returns false

4.delete() method is used to remove the specified element in the Map object

Syntax: myMap.delete(key)

Parameters:
key must be the key of the element removed from the Map object

Return value: If the element exists in the Map object, remove it and return true; otherwise If the element does not exist, return false

let myMap = new Map();
myMap.set("bar", "foo");
myMap.delete("bar"); // 返回 true。成功地移除元素
console.log(myMap.size); // 0

5. The clear() method will remove all elements in the Map object

Syntax: myMap.clear()

let myMap = new Map();
myMap.set("bar","baz");
myMap.set(1,"foo");
console.log(myMap.size); // 2
myMap.clear();

6.entries()

Syntax: myMap.entries()

Return value: Return a new pair containing [key, value] Iterator object, the iteration order of the returned iterator is the same as the insertion order of the Map object

let myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");
let mapIter = myMap.entries();
console.log(mapIter.next().value); // ["0", "foo"]
console.log(mapIter.next().value); // [1, "bar"]
console.log(mapIter.next().value); // [Object, "baz"]

7.keys() returns a new Iterator object. It contains the key value of each element inserted in the Map object in order

Syntax: myMap.keys()

let myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");
let mapIter = myMap.keys();
console.log(mapIter.next().value); // "0"
console.log(mapIter.next().value); // 1
console.log(mapIter.next().value); // Object

8.values() method returns a new Iterator object. It contains the value of each element inserted in the Map object in order

Syntax: myMap.values()

let myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");
let mapIter = myMap.values();
console.log(mapIter.next().value); // "foo"
console.log(mapIter.next().value); // "bar"
console.log(mapIter.next().value); // "baz"

9.forEach()

Syntax: myMap.forEach(callback[, thisArg])

Parameters:

callback is required, the function to be executed for each element
thisArg is optional, when callback is executed The value of this

let myMap = new Map([["foo", 3], ["bar", {}], ["baz", undefined]]);
myMap.forEach((value,key,map) => {
console.log("key =",key,",value =",value); //key = foo ,value = 3
});

The above is the detailed content of Parsing Map objects in JavaScript (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete