0% found this document useful (0 votes)
153 views9 pages

Set & Map in LWC

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
153 views9 pages

Set & Map in LWC

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
S ~*~ G&G ic > = © (7 \\ v ~ g Set & Map in LWC A \ F € Map collection in LWC Map is a collection of key-value pairs where the key can be of any type. Map remembers the original order in which the elements were added to it, which means data can be retrieved in the same order it was inserted in. //initilize the map collection in LWC JS const map = new Map(); //We can also initilize the map with existing data let map] = new Map({[[1, “Ankit”], [2 , “Rijwan”] ,[3, “Himanshu"]}); # Add values to a Map in Use the set(key, value) method for add key value in map const map = new Map(); map.set(‘name’, ‘Map in LWC’); map.set(‘type’, blog’); map.set(‘writer’, ‘Rijwan’); console.log(map); // output Map(3) {"name” => “Map in LWC", “type” => “blog”, “writer” => "Rijwan’} map.set(‘writer’, ‘Himanshu’); # Get values to a Map in let map] = new Map([[1, “Ankit”], [2 , “Rijwan”] ,[3, “Himanshu"]]); console.log(map! .get(2)); // Rijwan console.log(‘size of the map is’, map.size); if(map].has(2)){ console.log(‘key 2 is present’); // key 2 is present } 03 | Map collection in LWC Map collection in LWC # Remove values from a Map let map] = new Map({[1, “Ankit’], [2 , “Rijwan’] ,[3, “Himanshu"}]); mapl.delete(1); mapl.clear(); # get/ print all keys & values from a Map let map] = new Map([[1, “Ankit”], [2 , “Rijwan’] ,[3, “Himanshu"]]); let keys = map].keys(); console.log(keys); // { 1, 2,3} let allValues = mapl.values(); console.log(allValues); //{ ‘Ankit’, ‘Rijwan’, ‘Himanshu’ } # Iterate Over a Map let map] = new Map({[1, “Ankit’], [2 , “Rijwan’] ,[3, “Himanshu"}]); map].forEach((value, key) => { console.log(*${key} is ${value} years old!*); Ds # Convert Map into an Array let map] = new Map([[1, “Ankit”], [2 , “Rijwan”] ,[3, “Himanshu"]]); console.log(Array.from(map!)); //[ [ ‘milk’, 200 J, [ ‘tea’, 300 J, [ ‘coffee’, 500 ] J 04 | Map collection in LWC Map collection in LWC import { LightningElement } from ‘lwe’; export default class LWCMapCollection extends LightningElement { handleMapInLWC(){ //Define a map in LWC let map] = new Map([[1, “Ankit"], [2 , “Rijwan"] ,[3, “Himanshu"]]); //Check whether map contains key. ==> has(key) if(map1.has(2)){ console.log(‘key 2 is present’); } // fetch value by key ==> mapl.get(key) console.log(map1.get(1)); //set key and value in map ==> set(key,value) mapl.set(4, “Sachin"); //delete(key) - delete given key from map. mapl.delete(4); //clear() - clear whole map. mapl.clear(); //keys() ==> print all keys console.log(map].keys()); // print all values of map =>values() console.log(map!.values()); //\oop on map mapl.forEach((value, key, map)=>{ console.log(‘Key ->‘ + key +‘ value ->‘ + value); yD; 05 | Map collection in LWC Set collection in LWC A Set is a collection of unique elements that can be of any type. Set is also an ordered collection of elements, which means that elements will be retrieved in the same order that they were inserted in. const set = new Set(); # Add element in Set let set] = new Set(); setl.add(1); setl.add(2); setl.add(2); console.log(setl); // { 1, 2 } # Remove element from Set let set] = new Set([Y, ‘2’, 3’, '4'1); setl.delete(‘2'); console.log(setl); // { ‘1’, ‘3’, '4°} set.clear(); // {} # has(element) from Set let set] = new Set(['Y, ‘2’, ‘3’, '4']); console.log(saladSet.has('2’)); // true console.log(saladSet.has(‘6’)); // false 06 | Set in LwC Set in LWC # Convert Set into an Array let set] = new Set(['T’, ‘2’, ‘3’, ‘4’]); const arr = [...set]]; console.log(arr); //[ ‘I’, '2','3','4'] # Iterate Over a Set const set] = new Set([360, 567, 101]); setl.forEach((value) => { console.log(value); Ds 07 | Set in LWe Set collection in LWC import { LightningElement } from ‘lwe’; export default class LWCUsefulMethod extends LightningElement { handlSetInLWC(){ //add unique values in array use set() let set] = new Set(); setl.add(1); setl.add(2); setl.add(2); setl.add(3); setl.add(3); //\oop on set set].forEach(function(value, valueAgain, set){ console.log(value); dD; setl.delete(‘2’); console.log(saladSet.has(1)); // true console.log(saladSet.has(1)); // false setl.clear(); //clear whole set collection 08 | Set Collection in LWC uta ae TECHDICER KEEP LEARNING AND SHARING KEEP LEANING AND SHARING

You might also like