JavaScript Map Object [ES6]

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
557
Reaction score
796
First Language
English
Primarily Uses
RMMV
You can find the original post here.

The Map Object
In ES6, JavaScript added a new Map class. The Map class is very similar to the standard object class, with a few differences. Today, we’re going over when to use and why to use them.

Why Map
Map, unlike the object class, does not have extra keys that have to be enumerated. This saves time when iterating over values in the map. The second thing is that a map can have a key of any type where an object’s key must be a string. Lastly, a map has built-in functions for getting values.

The key functions of a map are set, get, has, and delete. “set” adds the key-value pair to the map. “get” retrieves the value of the key. “has” checks if the key exists. Lastly, “delete” removes the key-value pair from the map without error. Each function provides a lot of value to the map class, which is better than spending time coercing an object to do the same job; this makes maps very useful.

These three things make the map extremely useful in situations where key-value pairs need to be stored. The built-in functions add flexibility that a basic object can’t provide. Here’s an example of using a map vs using an object.

JavaScript:
// Map Example

const a = new Map();
const b = {};

//Adding key-value pairs
console.log("Set Key");
//Map
a.set('test', 3);
console.log(...a.entries()); //[ 'test', 3 ]
//Object
b['test'] = 3;
console.log(b); //{ test: 3 }
//Get key
console.log("\nGet Key");
//Map
const c = a.get('test');
console.log("Map:", c);//Map: 3
//Object
const d = b['test'];
console.log("Object:", d); //Object: 3


//Has key
console.log("\nHas Test");
//Map
console.log("Map:", a.has('test')); //true

//Object
console.log("Object:", b['test'] !== undefined); //true (But could be set to null or undefined by developer)

//Remove key
console.log("\nDelete Key");
//Map
console.log("Map:", a.delete('test')); //true if there is a key to remove
console.log("Map:", a.get('test')); //Map: undefined
//Object
delete b['test'];
console.log("Object:",b['test']); //Object: undefined
So, that’s why to use a map, so what about when?


When To Use Map
A map should be used in select areas of your code. Given the “why”, they should be used when you need to store key-value pairs while the program is running. This is because maps can have keys that can’t be serialized properly in JSON. Furthermore, they would have to be coerced before being serialized. Although it’s not hard, if you only use strings as keys; using more complex keys will be trouble. So, use a map when you need to store key-value pairs that won’t be serialized. With that said, the map object provides a ton of value.

Conclusion
Maps are a great addition to the JavaScript language and have many uses. Although they shouldn’t be serialized, they come with built-in functions that make getting and looking up key-value pairs simple. The next time you think of using an object for storage, consider a map. Happy coding!

Sources
Map
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,850
First Language
English
Maps are nice. I alway use them in ruby, and in ES5 they didn't have a map type so it was kind sad. Object sort of worked but at the same time, didn't really have a nice interface to work with.

I generally avoid using mutable objects as keys. Stick with things like numbers and strings. Use actor ID's instead of Actor objects, etc.
 

Kino

EIS Game Dev
Veteran
Joined
Nov 27, 2015
Messages
557
Reaction score
796
First Language
English
Primarily Uses
RMMV
I agree with this 100%. My opinions have changed over the years and I definitely prefer immutable code where possible including in Maps!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

To composers out there, what are atleast 4 instruments overall I need to have to make my music not feeling empty? I'm here stuck trying to make my own battle music but it kinda sounds... Not intense?
My computer broke yesterday, so no RPG maker for at least a week until I can get it repaired. :(
Just wanted to share how my game won 1st place in a game jam for itch.io. Everyone else used Unreal, Unity, GMS2, Godot, etc but I used RPG Maker 2003 :guffaw: It just shows that you can create a good game and it doesn't matter which engine you use
Working on cutscenes~

if your game is the first (or ONLY) in a series and still has a colon in its title, you are a bad person and I hate you...... jk.

who could ever hate BETWEEN: TWO SOULS?

Forum statistics

Threads
107,405
Messages
1,029,444
Members
139,504
Latest member
lobsterknees
Top