O'qilgan sanalarni saqlang
muhimlik: 5
Oldingi topshiriq kabi bir xabarlar massivi mavjud. Vaziyat shunga o’xshash.
let messages = [
{ text: "Hello", from: "John" },
{ text: "How goes?", from: "John" },
{ text: "See you soon", from: "Alice" },
];
Endi savol tug’iladi: “xabar qachon o’qilgan?” ma’lumotni saqlash uchun qaysi ma’lumotlar tuzilishini taklif qilasiz.
Oldingi vazifada biz “ha/yo’q” faktini saqlashimiz kerak edi. Endi biz sanani saqlashimiz kerak va agar xabar yo’qolsa, yana ma’lumot yo’qoladi.
Sana saqlash uchun biz foydalanishingiz mumkin WeakMap
:
let messages = [
{ text: "Hello", from: "John" },
{ text: "How goes?", from: "John" },
{ text: "See you soon", from: "Alice" },
];
let readMap = new WeakMap();
readMap.set(messages[0], new Date(2017, 1, 1));
// Date obyektni biz keyinroq o'rganib chiqamiz