Typescript is providing with collections:
1.Map collection
2.Weak Map collection
3.Set collection
4 Weak set Collection
Map collection:
*Map is a collection of key and value pairs, key should be an unique *key and value can be of any type
Syntax: let <mapobject>=new Map();
Methods
1.Set(key, value)-It will add | modify an element of map collection, it will return map object key is not existing-->adding key is existing -->modifying 2.get(key) It will return a value based on key 3.delete(key) It will delete an element based on key 4.has(key) It will verify whether element based on key is present or not, it will return true | false
true--existing false--not existing
5.size It will return collection count 6.keys() It will return keys collection 7.values() It will return values collection 8.entries() It will return key and value collection
Example
Output
Map (4) {1 => "angular", 2 => "reactjs", 3 => "nodejs", 4 => "azure"}
Map (4) {1 => "angular17", 2 => "reactjs", 3 => "nodejs", 4 => "azure"}
false
1
2
3
"angular17"
"reactjs"
"nodejs"
1, "angular17"
2, "reactjs"
3, "nodejs"
WeakMap:
Weak map is a collection of key and value pairs,
key should be an unique
key should be an object and value can be of any type.
Setting key to null, will remove corresponding element automatically from weak map collection, this will not happen with map collection
Methods[same as Map collection]: 1.set(key,value) 2.get(key) 3.delete(key)
Example:
let weakmap=new WeakMap();let user1={name:"charan"};//object[key]let msgs1=["hi","how r u"];//valuelet user2:string | nulluser2={name:"raju"};let msgs2=["fine","thank u"];weakmap.set(user1,msgs1);weakmap.set(user2,msgs2);console.log(weakmap.get(user1));console.log(weakmap.get(user2));
Output
["hi", "how r u"]
["fine", "thank u"]Set: ---- Set is a collection of unique values, value can be of any type. Set is similar to an array, but array allows duplicate values, where as set allows unique value
Array will have restricted size, where as set will not have size restriction
Methods: 1.add(value)--it will add an element into set collection 2.delete(value) 3.values()--it will return values collection
Example
let set=new Set();set.add("hyd").add("bang").add("chennai").add("noida");console.log(set);set.add("hyd");//it will not addconsole.log(set);let arr=Array.from(set);//it will create an array based on setarr.push("hyd");//it will add an element to an array,it will not reflect on setconsole.log(arr);console.log(set);OutputSet (4) {"hyd", "bang", "chennai", "noida"}
Set (4) {"hyd", "bang", "chennai", "noida"}
["hyd", "bang", "chennai", "noida", "hyd"]
Set (4) {"hyd", "bang", "chennai", "noida"}Note ----- Collections are provided with generic to specify particular datatype map()[non generic] map<type,type>()[generic] set()[non generic] set<type>() [generic] let set1=new Set<number>(); [set can accept only unique numbers] WeakSet: -------- Weak Set is a collection of unique values, value should be an object
Setting value to null, will remove corresponding element automatically from weak set collection.
No comments:
Post a Comment
Thank you for visiting my blog