JavaScript Map
A much cleaner and performant alternative to objects.
const myPokemons = new Map();
const bulbasaur = { id: 1, name: "Bulbasaur" };
myPokemons.set(bulbasaur.id, bulbasaur);
myPokemons.delete(bulbasaur.id);
Iterating
for (const [key, value] of myPokemons) {
// ...
}
- Cleaner to iterate compared to objects.
- Iterates in the order of entry insertion.
Iterators
// Iterator of all map keys
myPokemons.keys();
// Iterator of all map values
myPokemons.values();
Cloning
const clonedPokemons = new Map(myPokemons);
// deep cloning
const deepClone = structuredClone(myPokemons);
Converting to objects
const myObj = Object.fromEntries(myMap);
Converting to Map
const myMap = new Map(Object.entries(myObj));
Use any type of object as keys
myMap.set(document.body, value);
myMap.set(function() {}, value);
myMap.set(myPokemon, value);