const weakMap = new WeakMap();
const obj1 = {};
const obj2 = {};
weakMap.set(obj1, 'value1');
weakMap.set(obj2, 'value2');
const arr = [obj1, obj2];
const result = arr.map(obj => weakMap.get(obj)).join(', ');
console.log(result);
output : value1, value2
20 YouTube channels for web development:
1. Traversy Media
2. The Net Ninja
3. freeCodeCamp
4. Academind
5. Code with Harry
6. Programming with Mosh
7. Dev Ed
8. Wes Bos
9. Chris Courses
10. DesignCourse
11. Web Dev Simplified
12. Florin Pop
13. Kevin Powell
14. LevelUpTuts
15. Tech with Tim
16. Coding Addict
17. Dorian Develops
18. Web Dev Cody
19. Coder Coder
20. Ben Awad
1. Traversy Media
2. The Net Ninja
3. freeCodeCamp
4. Academind
5. Code with Harry
6. Programming with Mosh
7. Dev Ed
8. Wes Bos
9. Chris Courses
10. DesignCourse
11. Web Dev Simplified
12. Florin Pop
13. Kevin Powell
14. LevelUpTuts
15. Tech with Tim
16. Coding Addict
17. Dorian Develops
18. Web Dev Cody
19. Coder Coder
20. Ben Awad
CHALLENGE
function* idGenerator() {
let id = 1;
while (true) {
yield id++;
}
}
const gen = idGenerator();
const weakMap = new WeakMap();
const objs = [{}, {}, {}];
objs.forEach(obj => weakMap.set(obj, gen.next().value));
const result = objs.map(obj => weakMap.get(obj)).filter(id => id % 2 === 0);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const obj = {};
(function() {
const obj1 = { name: 'inner' };
weakMap.set(obj1, 'inner value');
})();
const result = weakMap.get(obj);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const obj = {};
const gen = (function* () {
yield 'value1';
yield 'value2';
})();
weakMap.set(obj, gen.next().value);
console.log(weakMap.get(obj));
console.log(gen.next().value);
CHALLENGE
function* evenNumbers() {
let num = 0;
while (true) {
yield num;
num += 2;
}
}
const gen = evenNumbers();
const evens = Array.from({ length: 4 }, () => gen.next().value).map(n => n + 1);
console.log(evens);
CHALLENGE
const weakMap = new WeakMap();
const gen = (function* () {
yield { key: 'value1' };
yield { key: 'value2' };
})();
const obj1 = gen.next().value;
const obj2 = gen.next().value;
weakMap.set(obj1, 'stored value1');
weakMap.set(obj2, 'stored value2');
const result = [...gen].map(obj => weakMap.get(obj));
console.log(result);
SmartCode
CHALLENGE const weakMap = new WeakMap(); const gen = (function* () { yield { key: 'value1' }; yield { key: 'value2' }; })(); const obj1 = gen.next().value; const obj2 = gen.next().value; weakMap.set(obj1, 'stored value1'); weakMap.set(obj2, 'stored…
Output :
Anonymous Quiz
50%
[]
8%
[null,null]
33%
[Undefiend,Undefined0]
8%
[stored value1, stored value2]
CHALLENGE
const weakMap = new WeakMap();
const array = [{}, {}];
array.forEach(obj => weakMap.set(obj, obj));
const result = array.map(obj => weakMap.get(obj) === obj);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const obj = {};
(function() {
const internalObj = {};
weakMap.set(internalObj, 'hidden');
obj.ref = internalObj;
})();
delete obj.ref;
const result = weakMap.has(obj.ref);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const array = [1, 2, 3];
const obj = {};
weakMap.set(obj, array);
const result = weakMap.get(obj).reduce((acc, val) => acc + val);
console.log(result);
CHALLENGE
const weakMap = new WeakMap();
const objs = [{}, {}, {}];
objs.forEach((obj, index) => weakMap.set(obj, index + 1));
const result = objs.filter(obj => weakMap.has(obj)).map(obj => weakMap.get(obj) * 2);
console.log(result);