// ES6: Sets and Maps
// Sets
const certifications = new Set([
'Admin', 'PD I', 'PD II', 'Admin', 'AppBuilder'
]);
console.log(certifications);
console.log(certifications.size); // It should be size not length
// Verify value
console.log(certifications.has('Admin'));
console.log(certifications.has('Sales Cloud'));
// Adding Values
certifications.add('Salesforce CPQ')
console.log(certifications);
// Deleting values
certifications.delete('Salesforce CPQ')
console.log(certifications);
// Iteration
for(const cer of certifications) console.log(cer);
// Clear all the values
certifications.clear();
console.log(certifications);
// *** Making Array as Set with the unique values
const names = ['Mark', 'Steve', 'Mark', 'Bob'];
const uniqueNames = [...new Set(names)];
console.log(uniqueNames);
// size of array with unique values
console.log(new Set(['Mark', 'Steve', 'Mark', 'Bob']).size);
// Size of a string with unique characters
console.log(new Set('Steve').size);
--Maps----------------
// Maps are like objects but map keys can be any data type (object keys always should be string)
const resMap = new Map();
resMap.set('firstName', 'Srinu');
resMap.set(1, 'Admin');
console.log(resMap.set(2, 'Dev'));
resMap
.set('certifications',['Admin','PD I'])
.set('city','Bangalore')
.set(true,'He is a Trainer.');
console.log(resMap.get('firstName'));
console.log(resMap.get(resMap.get('firstName') === 'Srinu'));
console.log(resMap.has('lastName'));
resMap.delete('city');
console.log(resMap);
console.log(resMap.size);
resMap.clear();
console.log(resMap);
// resMap.set([1,2],'nums'); // won't work
// console.log(resMap.get([1,2])); // won't work
const myArr = [1,2];
resMap.set(myArr,'nums');
console.log(resMap.get(myArr));
// Map Iteration (Map entries are set of arrays)
const question = new Map([
['question', 'Which is the #1 CRM Platform?'],
[1, 'Microsoft'],
[2, 'Salesforce'],
[3, 'Zoho'],
['correct', 2],
[true, 'Correct'],
[false, 'Try Again']
]);
console.log(question);
const workTimings = {
tue : {
startTime : 0,
endTime : 8,
},
wed : {
startTime : 8,
endTime : 4,
},
thu : {
startTime : 8,
endTime : 4,
},
fri : {
startTime : 8,
endTime : 4,
}
};
// Converting Object to map
console.log(Object.entries(workTimings));
const timingsMap = new Map(Object.entries(workTimings));
console.log(timingsMap);
// Question Iteration
console.log(question.get('question'));
for(const [key,value] of question) {
if(typeof key === 'number') console.log(`Answer ${key}: ${value}`);
}
//const answer = Number(prompt('Enter Answer'));
const answer = 2;
console.log(answer);
console.log(question.get(question.get('correct') === answer));
// convert map to array
console.log([...question]);
console.log([...question.keys()]);
console.log([...question.values()]);
--When to choose which collection -----------------------
Data Structures -
1. Arrays or Sets --> Simple List
2. Objects or Maps --> Key/Value Pair
* Set is faster than Array
* Map is faster than Object
* For Json we use objects
No comments:
Post a Comment