Using Array objects as key for ES6 Map

前端 未结 3 1508
故里飘歌
故里飘歌 2020-12-06 09:42

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

3条回答
  •  旧巷少年郎
    2020-12-06 10:35

    You need to save a reference to the non-primitive instance of Array you used as a key. Notice the difference in the following two examples:

    "use strict";
    
    var a = new Map();
    a.set(['x','y'], 1);
    console.log(a.get(['x','y']));
    console.log(['x','y'] === ['x','y']);
    
    var b = new Map();
    var array = ['x','y'];
    b.set(array, 1);
    console.log(b.get(array));
    console.log(array === array);

提交回复
热议问题