Map

map & set in script 6

by Samar Pattanayak

JavaScript

var map= new Map();
map.set(1,"name");
map.set(2,"age");
console.log(map.size);
map.set(2,"Oldage");//it will not through any error if you add an existing key , it will ignore that one.
map.set(3,"will delete");
console.log(map.entries());
for(let [key,value] of map){
	console.log(key + " = " + value);
}
for(let key of map.keys()){
	console.log(key + ' = '+ map.get(key));
}
for(let [key, value] of map.entries()){
	console.log(`${key} = using ec6 ${value}`);
}
map.forEach(function(value, key) {
  console.log(`${key} = using ec6 foreach ${value}`);
}, map)
//console.log(map.get("name"));
console.log(map.get(1));
console.log(`Current size is ${map.size}`);
console.log(`key 3 is present or not : ${map.has(3)}`);
console.log(`key 4 is present or not : ${map.has(4)}`);
//map.delete(3);
console.log(`Current size after deleting 3 is : ${map.size}`);
//map.clear();
console.log(`Current size after clearing all is : ${map.size}`);
//Array in map;
var arrmap=[["key1","value1"],["key2","value2"]];
var arrmapObject= new Map(arrmap);
console.log(arrmapObject.entries());
//console.log(uneval([...arrmapObject]));//Will show you exactly the same Array as arrmap.
console.log(`*************Map Complete************`);
var mySet = new Set();
mySet.add(1);
mySet.add(5);
//mySet.add(5);//dont throw any error.
mySet.add("some text");
console.log(mySet.entries())//it will display in [key, value] structure and here key and value will be same.
console.log(mySet.size);






//object example here
var obj={1:234,2:345};
console.log(`Object iteration DEMO here `)
for(var key in obj){
	console.log(`${obj[key]}`)
}
//array looping here
var arr=[1,2,3,4];
console.log(`Array iteration DEMO here `)
arr.forEach(function(value,index){
	console.log(` ${index}  - ${value}`);
})
for(let i of arr.values()){
	console.log(`Array ${i}`);
}
for(let [key,value] in obj){
	console.log(`Object ${key}`)
}