turtle
turtle

Reputation: 8073

How to get and set keys which are arrays in ES6 Maps?

I have an ES6 Map that looks like this:

let m = new Map([['a', 1], [['a', 'b'], 1]])

I can get string values like this:

m.get('a') // this value is 1

However, I can not get values which are arrays:

m.get(['a', 'b']) // this value is undefined

How can I get keys from a map which are arrays?

Upvotes: 0

Views: 50

Answers (1)

Łukasz Karczewski
Łukasz Karczewski

Reputation: 1208

try this:

const arr = ['a', 'b'];
let m = new Map([['a', 1], [arr , 1]]);
console.log(m.get(arr));

Maps use reference equality when retrieving values.

Upvotes: 4

Related Questions