Skip to content
Advertisements

JavaScript Map

Why Map ?

The JavaScript Get () method is a feature that is supported by ES6. The task of the Get () method is to store certain items. The items that it stores may be primitive values ​​or key values. When we repeat on the map object, it returns the key, the value pair being the same as the entered order.

Syntax:

myMap.get( value );

  • value (required): The value to return in the JavaScript Map object.

Example 1:

        let myMap = new Map();
        myMap.set("first", "1.");
        myMap.set("second", "2.");
        myMap.set("third", "3.");

        console.log(myMap.get("first"));
        console.log(myMap.get("second"));
        console.log(myMap.get("third"));
        console.log(myMap.get("forth"));
        console.log(myMap.get("fifth"));

1.
2.
3.
undefined
undefined

Example 2:

        let myMap = new Map([
            ["name: ", "Alex"],
            ["surname: ", "Allen"],
            ["age: ", 24],
            ["from", "England"]
        ]);

        console.log(myMap);

{“name: ” => “Alex”, “surname: ” => “Allen”, “age: ” => 24, “from” => “England”}

Example 3:

        let myMap = new Map([
            ["positive numbers: ", [1, 2, 3, 4, 5]],
            ["negative numbers: ", [-1, -2, -3, -4, -5]],
        ]);
        console.log(myMap)

positive numbers => [1, 2, 3, 4, 5]
negative numbers => [-1, -2, -3, -4, -5]

Map.prototype.size

The Map.prototype.size () property returns the number of items in a map object. A set accessor function for size is undefined; you can not change this property.

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        console.log(myMap.size);

4

Map.prototype.clear()

The Map.prototype.clear () method removes all items in the object from the Map object.

Syntax:

myMap.clear()

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        console.log(myMap.size);

        myMap.clear();
        console.log(myMap.size);

4
0

Map.prototype.delete()

The Map.prototype.delete () method removes the specified item from the object.

Syntax:

myMap.delete(value)

  • value: the value to be removed from the object is entered.

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        console.log(myMap);

        myMap.delete("person2");
        console.log(myMap);

        myMap.delete("person num");
        console.log(myMap);

Map(4) {“person1” => “alex”, “person2” => “rick”, “person3” => “oliver”, “person num” => 3}
Map(3) {“person1” => “alex”, “person3” => “oliver”, “person num” => 3}
Map(2) {“person1” => “alex”, “person3” => “oliver”}

Map.prototype.entries()

It returns an iterator object that contains key/value pair for each element present in the Map object.

Syntax:

myMap.entries()

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        let arr = myMap.entries();

        console.log(arr.next().value);
        console.log(arr.next().value);
        console.log(arr.next().value);
        console.log(arr.next().value);

[“person1”, “alex”]
[“person2”, “rick”]
[“person3”, “oliver”]
[“person num”, 3]

Map.prototype.forEach()

 It executes the callback function once for each key/value pair in the Map, in the insertion order.

Syntax:

myMap.forEach(callback[, thisArg])

  • callback: It is a function which is to be executed for each element of the Map.
  • thisargument: Value to be used as this when executing callback.
  • callback is invoked with three arguments:
  • the element value
  • the element key
  • the Map object being traversed

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        function print(value) {
            console.log(value)
        };

        myMap.forEach(print);

alex
rick
oliver
3

Map.prototype.get()

Map.prototype.get() returns the value of the corresponding key.

Syntax:

myMap.get( value )

  • value (required): The key of the element to return from the Map object.value:

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        arr1 = myMap.get("person1");
        console.log(arr1);

        arr2 = myMap.get("person2");
        console.log(arr2);

        arr3 = myMap.get("person3");
        console.log(arr3);

        arr4 = myMap.get("person num");
        console.log(arr4);

alex
rick
oliver
3

Map.prototype.has()

The Map.prototype.has () method checks whether the entered value exists. Returns the boolean value accordingly. If the entered value is present in the object, it returns true, and the entered value returns false if it is not present in the object.

Syntax:

myMap.has( value )

  • value: Value of the element to checked

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        control1 = myMap.has("person1");
        console.log(control1)

        control2 = myMap.has("person2");
        console.log(control2)

        control3 = myMap.has("person3");
        console.log(control3)

        control4 = myMap.has("person4");
        console.log(control4)

        control5 = myMap.has(3);
        console.log(control5)

true
true
true
false
false

Map.prototype.keys()

Map.prototype.keys() returns an iterator object which contains all the keys present in the Map Object.

Syntax:

myMap.keys()

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        let myKeys = myMap.keys();

        let arr1 = myKeys.next().value;
        console.log(arr1);

        let arr2 = myKeys.next().value;
        console.log(arr2);

        let arr3 = myKeys.next().value;
        console.log(arr3);

        let arr4 = myKeys.next().value;
        console.log(arr4);

person1
person2
person3
person num

Map.prototype.set()

Map.prototype.set () returns the number of items or key-values on the map.

Syntax:

myMap.set(key, value)

  • key: Key of the element to be added to the Map.
  • value: Value of the element to be added to the Map.

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        myMap.set("person4", "bob");
        myMap.set("person5", "maria");

        function print(value) {
            console.log(value);
        };

        myMap.forEach(print);

alex
rick
oliver
3
bob
maria

Map.prototype.values()

Map.prototype.values ​​() returns an iterator object that contains all the values ​​in the Map Object.

Syntax:

myMap.values()

Example:

        let myMap = new Map();

        myMap.set("person1", "alex");
        myMap.set("person2", "rick");
        myMap.set("person3", "oliver");
        myMap.set("person num", 3);

        myValues = myMap.values();

        function print(value) {
            console.log(myValues.next().value);
        };

        myMap.forEach(print);

alex
rick
oliver
3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: