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 run this code and I would like to know how I can use arrays as keys for the Map
.
"use strict";
var a = new Map();
a.set(['x','y'], 1);
console.log(a.get(['x','y']));
var b = {};
b[['x','y']] = 1;
console.log(b[['x','y']]);
It prints out the following and the first line should be 1
and not undefined
:
undefined
1
The original JS map stringifies the key and I am not wanting to do the same type of stringify hack with the new ES6 Map
.
What can I do to use arrays as keys reliably for a ES6 Map
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…