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
0
votes
1 answer

Check if a string is contained within a specific Map() key

I have a map which i am populating from a json file, similar to the below key: ConfigItem value: Var1,Var2,Var3 key: ConfigItem2 value: Var1,Var2,Var3,var4 key: ConfigItem3 value: true i want to be able to run an if statement to check if a…
Red Spider
  • 25
  • 2
  • 6
0
votes
2 answers

Iterating through specified range within js es6 map

I have a sorted JavaScript map structure storing object states based on time. Map(key:time(inMilli), value: {object attributes}) What I am needing to accomplish is to be able to check map against a start and end time to get a collection of all…
Zachary Evans
  • 21
  • 1
  • 7
0
votes
7 answers

How to check if two Maps have the same key set in JavaScript

Suppose to have two Map objects, how to check if their keys sets are the same? For example: const A = new Map(); A.set('x', 123); A.set('y', 345); const B = new Map(); B.set('y', 567); B.set('x', 789); const C = new Map(); C.set('x',…
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
0
votes
1 answer

How can i pass a Map in angular 2+ from html template to custom directive?

I'm trying to pass a map from an angular html template to a custom directive. I guess I succeed to pass it as an object, because I can console.log the map values. But when I try to set or get values from the map I get an error. My code: html…
0
votes
2 answers

How to get content of Map object in JS template literal in readable format?

Let's say I have a Map: const m = new Map([[1,1], [2,2], [3,3]]); What's the quickest method to inline it in template literal in a manner that it will look like this: or any other readable format? P.S. This won't work: const str = `${m}`;
zmii
  • 4,123
  • 3
  • 40
  • 64
0
votes
1 answer

js how to implement map[index]++ concisely

With Array, I can use array[index]++ But with Map, I only know map.set(index,map.get(index)+1) I think it looks bad, and if index is a long name function, I have to split it into two lines. Is there a more concise way to implement map[index]++
bilabila
  • 973
  • 1
  • 12
  • 18
0
votes
1 answer

how can i handle my json elements

Here is my json file { id: '81224112234234222223422229', type: 'message', message: 'vacation', attachments: [ { type: 'template', elements: [ { …
james
  • 235
  • 2
  • 5
  • 11
0
votes
2 answers

how to remove character from array using filters and map?

I am trying to remove specific characters from an array. I am passing a sentence,characters from that sentence should be removed from alpha array using map() and filters(), var alpha =['b','c','d','e','f','g','h','a'] function…
suchi
  • 83
  • 10
0
votes
0 answers

Proxy target: Map object

I'm trying to set a Map as the target of a Proxy, but I'm getting the following error: Uncaught TypeError: this.proxy.family.set is not a function I believe the error is related to both .set() method in both the Map and Proxy objects. function…
user7892649
0
votes
5 answers

React - TypeError: Cannot read property 'map' of undefined

I am building a Charts Gallery using the Recharts library. These are the files i am working on The data fixtures is charts.js const charts = [ {name: 'Page A', uv: 4000, pv: 2400, amt: 2400}, {name: 'Page B', uv: 3000, pv: 1398, amt: 2210}, …
Koala7
  • 1,340
  • 7
  • 41
  • 83
-1
votes
1 answer

Evaluate an array inside an ES6 map to actually print elements on console

Description I have an ES6 map. It has a string key, whose value is an array of maps. The following minimal reproducible code best describes it - var rootMap = new Map() rootMap.set('details', []) var subMap = new Map() subMap.set('items', ['foo',…
anon
-1
votes
3 answers

Format array of objects with varied data structures

I have the following data: const match = 'A' const data = [ { age: "12", group: [ {club: 'A'}, {club: 'B'} ], active: { sjbuas777: {club: 'A'}, 122kk7dgs:…
mtwallet
  • 5,040
  • 14
  • 50
  • 74
-1
votes
2 answers

javaScript ES6 map() return certain object only

javaScript ES6 .map() return expecting including location & city based on which activeMember: true in memberDetail const data = [ { id: 131131, title: "Completed members", memberDetail: { "0": { activeMember:…
Mo.
  • 26,306
  • 36
  • 159
  • 225
-2
votes
1 answer

How to loop over map and generate JSX in React?

This is my map: const map1 = new Map(); map1.set(0, 'foo'); map1.set(1, 'bar'); I want to generate a UI as 0: foo 1: bar How can I generate this UI? Thanks for the response. But basically i have structure const data = { title: "hello lorem…
Geeky
  • 7,420
  • 2
  • 24
  • 50
-2
votes
2 answers

Create a new result from an existing array

If I have a sample data that looks like this I need to get finalResult array from result array: let result = [{ type: ['Science'], link: "www.educatorsector.com" }, { type: ['Sports', 'News'], link: "www.skysports-news.com" }, …
teestunna
  • 197
  • 4
  • 17