Objects - stored String or Symbol keyed collection
Arrays - stored ordered collection
Map - stored any type keyed collection
Map methods
1. let map = new Map() - creates an empty Map
2. map.set(key, value) - stores the value in the key
3. map.get(key) - returns the value using the map key
Note: You can use the bracket notation in the set and get but it is not advisable.
4. map.has(key) - returns true if the key exists in the map
5. map.delete(key) - removes the value using the key
6. map.clear() - deletes every element inside the map.
7. map.size() - return the size or the length or the number of elements within the map collection.
8. map.keys() - returns a map collection of keys.
9. map.values() - returns a map collection of values.
10. map.entries() - returns a map collection of both keys and values known as entries.
11. let map = new Map([[key1, value1], [key2, value2] ... ]) - creating a map using an array within an array that is arranged in a key-value pairs.
12. let map = new Map(Object.entries(object)) - creating a map using an Object.entries() method. You pass in an object as an argument.
Object.entries(object) - returns an array of key-value pairs from an object
Object.fromEntries(map1) - returns an object from an array of key-value pairs
=====================
let map1 = new Map([
[1, "apple"], [2, "banana"], [3, "chery"], [4, "dairy"]
]);
let book1 = {
title: "Slime",
author: "Rimuru",
published: 2021
};
let map2 = new Map(Object.entries(book1));
let obj1 = Object.fromEntries(map1);
function displayTheCollections(){
for(let x of map1.entries()){
document.writeln(x);
}
document.writeln("*****");
for(let x of map2.entries()){
document.writeln(x);
}
document.writeln("*****");
for(let y in obj1){
document.writeln(obj1[y]);
}
}
=====================
HTML:
displayTheCollections()
Result:
1,apple 2,banana 3,chery 4,dairy ***** title,Slime author,Rimuru published,2021 ***** apple banana chery dairy
YOU ARE READING
Javascript Programming
RandomI am just documenting things what i learned online in here. I do express things in my own words. And I create my own program samples which serves as my practice. I do this so that when someday I would need some notes to review, I can just visit in h...
