DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for JavaScript Array Key as Object Type
Top
Top

Posted on • Updated on

JavaScript Array Key as Object Type

var a={},
 b={key:'b'};
a[b]=123;

Enter fullscreen mode Exit fullscreen mode

In this case, b as key is "[object Object]" - result of b.toString()
Therefore, every keys are same as "[object Object]" whenever Object passed into Array Key

In this case, we can solve this problem using ECMAScript 6 Maps

var a = new Map(),
    b = {key: 'b'};
a.set(b, 123);
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)

Welcome! What are you learning?

Each week we have a Welcome Thread where we'd love to hear about it!