Questions tagged [es6-map]

The ECMAScript 6 Map object holds key-value pairs that can be iterated through by looping. To use this tag, the post must be using ECMAScript 6 and must concern the use of how to use Map properly.

Syntax

To create a map:

const myMap = new Map();  //Map(0) 

This will create a new empty map object. The Map() constructor takes one optional parameter. For example, it can take an array of key/value pairs:

const myArr = [[1, 'one'], [2, 'two']];
const myMap = new Map(myArr);  //Map(2) {1 => "one", 2 => "two"}

This creates a map object with key 1 with a value of 'one' and key 2 with a value of 'two'. This is also possible with an iterable object:

const myIt = {
*[Symbol.iterator]() {
    yield [1, 'one'];
    yield [2, 'two'];
}};
const myMap = new Map(myIt); //Map(2) {1 => "one", 2 => "two"}

Again, this creates a map with the same structure as the last example.

Note that any value, from primitives to objects, can be used as either a key or a value. This includes NaN (Not a Number):

const myArr = [[NaN, 'one']];
const myMap = new Map(myArr);  //Map(1) {NaN => "one"}

This creates a map with a key of NaN with the value of "one". Even though NaNs are indistinguishable from each other, you cannot use them for multiple keys:

const myArr = [[NaN, 'one'], [NaN, 'two']];
const myMap = new Map(myArr);  //Map(1) {NaN => "two"}

This will only create one key of NaN where the last value associated with NaN will be in the map.

There are also getters and setters for the Map Object:

const myMap = new Map();  //Map(0)
myMap.set(1, "one");    //Map(1) {1 => "one"}
myMap.set(2, "two");    //Map(2) {1 => "one", 2 => "two"}
myMap.get(1);           //one
myMap.get(2);           //two

You can also create a map with mixed keys:

const myMap = new Map();
myMap.set(1, "one");
myMap.set("two", 2);
myMap.set(function () { return 3; }, "three")

This will create a map with a key of integer 1 with string value "one", a key of string "two" with an integer value of 2, and a key of an anonymous function with a string value of "three". However, while this works with setting, getting the last key will be impossible unless you define it outside the map first:

const myFunc = function () { };
const myMap = new Map();
myMap.set(1, "one");
myMap.set("two", 2);
myMap.set(myFunc, "three")
myMap.get(myFunc);    // three

To iterate through the map, you can use destructuring with the for...of loop:

for (var [key, value] of myMap) {
    console.log(key + ' = ' + value);
}

Or the forEach loop can be used as well:

myMap.forEach(function(value, key) {
    console.log(key + ' = ' + value);
});

Both examples will output the keys along with their values.

Reference

developer.mozilla.org

91 questions
45
votes
5 answers

Using Array objects as key for ES6 Map

I am trying to update my code to ES6 as I am using Node 4.0 and really like its features so far. However I have problems with the new ES6 Map data structure as it behaves differently to {} when using Array as a key. I am using it as a counter map. I…
jimjampez
  • 1,766
  • 4
  • 18
  • 29
29
votes
7 answers

Update the attribute value of an object using the map function in ES6

I am trying to code this in ES6. Below is what I am trying to achieve. Let's say I have an array of objects called schools. let schools = [ {name: 'YorkTown', country: 'Spain'}, {name: 'Stanford', country: 'USA'}, {name: 'Gymnasium…
Rito
  • 3,092
  • 2
  • 27
  • 40
9
votes
1 answer

Typescript: How can I make entries in an ES6 Map based on an object key/value type

I would like to use Map instead of object map to declare some keys and values. But Typescript doesn't seem to support index types for ES6 Map, is that correct and are there any workarounds? Additionally, I would like to make the values type-safe as…
9
votes
3 answers

Better way to call `Array.prototype.some()` on es6 Map

I recently converted some code that made use of regular objects as maps to the new es6 Map class. I encountered an issue pretty quickly as, while the Map class includes a forEach like Array, it does not include a some method along with many other…
KhalilRavanna
  • 5,768
  • 3
  • 28
  • 24
8
votes
3 answers

Javascript Map ordering

i recently had a bug in my code that was due to me missing the "in insertion order" text while looking through Map object details on MDN. In short, i have a map object, lets say let myMap = new Map; and then, after populating it, i iterate over its…
Pavel Beliy
  • 494
  • 1
  • 3
  • 14
8
votes
2 answers

Is there any difference between es6 map and immutable.js map?

I am new to immutable.js. Is there any difference between map from immutable.js and map from ES6? If there is no difference, why do we need to use immutable.js? immutable.js const { Map } = require('immutable') const map1 = Map({ a: 1, b: 2, c: 3…
user2396010
7
votes
3 answers

How to iterate through JavaScript Maps using ng-repeat in angularjs?

I'm trying to use JavaScript Maps in ng-repeat angular's directive while searching I found that it could be done this way :
Xsmael
  • 3,624
  • 7
  • 44
  • 60
6
votes
6 answers

Javascript map then filter unique array items

I know how to do both things separately, but I'm sure there must be a way to combine them. I have an array of categories, which I am extracting from an array of objects: this.videoCategories = this.videos.map(v => v.category); But of course there…
Steve
  • 14,401
  • 35
  • 125
  • 230
6
votes
4 answers

ES6 Map to return an array of object keys only

I'm trying to write a method which will help me return an array of the object keys of all the currencies. But, I'm stuck at a point where I get the complete array of objects with key, value pair. And yes, I primarily need to use ES6 methods. I…
Sunny
  • 902
  • 3
  • 18
  • 41
6
votes
1 answer

Q:ES6 map.keys() after map.delete(key)

when I try these code: const map=new Map([['a', 1],['b', 2],['c', 3],['d', 4],['e', 5]]); console.log(map.keys()); map.delete('a') console.log(map.keys()); the chrome console will show these: MapIterator {"a", "b", "c", "d", "e"} MapIterator {"c",…
俞常爽
  • 61
  • 1
6
votes
3 answers

Convert an array of objects with a unique id property to a Map

I have an array of objects, where each object has a unique member called id. How do I create a Map where the id if the Map's key?
Baz
  • 12,713
  • 38
  • 145
  • 268
5
votes
2 answers

Why does Map.has() return false for an Integer key that does exist?

I have a map that consists of several key : value pairs, and the keys are all integers (that are then of course stored as strings). However, I can't use Map.prototype.has("1") nor Map.prototype.has(1) to confirm a key exists within the map. How do I…
dehuff
  • 103
  • 7
5
votes
2 answers

React map always calls method with data of last element

I am making a social media app. I am looping through all the comments and showing it on UI. When I click on edit, always the last comment's text show up on input. I tried many different things, changed structure, used bind to bind the context, but…
Parth Chokshi
  • 642
  • 4
  • 16
5
votes
2 answers

Using ES6 Map with Flow type

I'm trying to wrap my head around flow and I struggle to make it work with ES6's Map Consider this simple case (live demo): // create a new map const m = new Map(); m.set('value', 5); console.log(m.get('value') * 5) flow…
Asaf Katz
  • 4,608
  • 4
  • 37
  • 42
4
votes
2 answers

Higher perf - Map.prototype.forEach or for..of?

Say I have: const m = new Map(); is there a performance difference between: for(let v of m){ } vs. m.forEach(v => {}); I assume Map.protoype.forEach uses an iterator just like for..of? I read here that's it safe to delete keys from the map…
user7898461
1
2 3 4 5 6 7