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

Top comments (0)

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!