Maps and Sets
Choose keyed maps and unique sets when arrays or plain objects do not match the data.
Map stores key-value pairs with keys of any type. Set stores unique values. Both preserve insertion order and expose clear size and iteration APIs.
Keyed data with Map
progress.js
const progress = new Map();
progress.set("html", 100);
progress.set("css", 45);
console.log(progress.get("css"));
console.log(progress.has("javascript"));
for (const [course, percent] of progress) console.log(course, percent);
Use a plain object for a record with known property names. Use Map for a changing keyed collection, non-string keys, or frequent additions and removals.
Unique values with Set
topics.js
const topics = new Set(["html", "css", "html"]);
topics.add("javascript");
const uniqueTopics = [...topics];
Objects are compared by identity, not by matching contents. Two separate { id: 1 } objects are two Set values.
Keep this
Use Map for dynamic keyed collections, Set for uniqueness, and convert deliberately when storing or transmitting them.
学习位置已保存在此设备上。