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

HashMap in TypeScript with custom object

Introduction I am currently work in project that I need to save the score of each user. For this I used a Map to represent it. Problematic If I create map with a user named john: let myMap: Map = new Map(); myMap.set(new…
ThrowsError
  • 1,169
  • 1
  • 11
  • 43
0
votes
0 answers

Angular - Make Service return ES6 Map

I'm struggling with the ES6 Map Datatype in my HTTP-Service. In my Service I return an Observable, containing a Map: getLayout(dims: Map, rels: [number, number][]): Observable> { // ES6…
TehQuila
  • 628
  • 1
  • 8
  • 19
0
votes
3 answers

Filter an array on array of arrays of objects by matching a property of objects

I have a case where I have Array of Arrays and each inner array has multiple objects as below: const arrayOfArraysOfObjects = [ [ { name: 'abv', id: '1212' }, { name: 'gfgf', id: '887' }, { name: 'John', id: '886' } ], [ {…
Noob
  • 45
  • 3
  • 9
0
votes
1 answer

Save TypeScript class object to Firestore using the Node.js Admin SDK

I am trying to set data in a Firestore document from an object of a TypeScript class - class Quest { id: number = Date.now(); index: number = 0; quest: string[]; options = new Map(); goals: string[]; …
0
votes
2 answers

Javascript Map vs array of tuples

I just want to understand how javascript ES6 Maps (i.e. let m = new Map()) has 0(1) lookup time. My understanding of ES6 Maps is that its data structure is based on an array of tuples. You can even use an array of tuples in the constructor for…
Eric Grossman
  • 219
  • 1
  • 4
  • 9
0
votes
3 answers

map function doesn't work in React component

I'm currently learning ReactJS and I can't spot what is wrong with my code. So, I have a function and map() method inside. I have written it in 2 ways(arrow and normal) but the normal function doesn't seem work(page reloads empty but no errors or…
Dev
  • 355
  • 1
  • 7
  • 24
0
votes
1 answer

Cannot map value of object seen in console

My idea is to click on any of the buttons on the left navbar and whenever the name of the clicked button in the logos object matches any of the names in the items object within projects, then display those objects. When I click on any of the buttons…
0
votes
0 answers

initializing Map from JSON in TypeScript

I have been trying to initialize a Map type from a JSON blob, when I have a defined type for the expected JSON response. For example, we have a t-shirt size type. This is how the JSON blob looks like: const blob = `{ "name": "TShirtSize", …
negative0
  • 459
  • 4
  • 10
0
votes
0 answers

How to map through a nested object in ReactJS and display all keys and values

I am trying to display a list of fields that have been changed upon saving. To do this, I need to map through a very complex object consisting of both objects and arrays. The issue is in this part of the object right now: suppressionRules: { …
Alyssa
  • 143
  • 3
  • 15
0
votes
2 answers

React - Calculating the bigger value between elements only working partially

I am comparing two different Teams to define which is the stronger. The comparison is made by calculating the average of the two team score, the team with the greater value is the stronger. I have reproduced my case here: DEMO These are the…
Koala7
  • 1,340
  • 7
  • 41
  • 83
0
votes
0 answers

How do I make a typed key-value array into an ES6 map in TypeScript?

I've seen a lot of questions about this but the answers always involve array literals, which is not helping me... For example I've got a class like interface Foo { [key: string]: Bar; } And I'd love to be able to do something like function…
StolenKitten
  • 337
  • 1
  • 10
0
votes
1 answer

ES6 Map, showing exception intermittently , Uncaught TypeError: map.put is not a function

Few weeks back map.put used to work, but now we get this error message intermittently : var mapTypeSubtype = new Map(); Uncaught TypeError: mapTypeSubtype.put is not a function at Test.js:411 at Array.forEach () at…
Nagendra Singh
  • 577
  • 1
  • 7
  • 24
0
votes
0 answers

Need a little help nesting variables in ES6 backticks

I am trying to dynamically create a html UL with my own json data and a map function incorporating js ES6 backticks. I am almost there but I'm stuck with nesting a value in a variable. I have tried escaping the nested tool but no success let…
Al_Smith
  • 13
  • 5
0
votes
3 answers

Issue with rendering React JSX using if conditional inside map

case 1) I am trying to conditionally render using map and a ternary operator. Here I am iterating over an array of objects and then render only first six of them. Trying to achieve the same, but it does not seem like working. Help would be…
joy08
  • 9,004
  • 8
  • 38
  • 73
0
votes
1 answer

"no-unused-expressions" error in react with map function

I made an 'App' class and passed a state as props in a child 'Todos' component. Have a look at my code: class App extends Component { state = { todos:[ { id:1, title:'task1', complete:false, },{ id:2, …
ShahEryar Ahmed
  • 115
  • 1
  • 2
  • 10