Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON object as key in key-value pair

I see this isn't possible (in JavaScript), would this be due to serialization for persistance?

ActionScript3 does allow object instances as keys flash.utils.Dictionary

Then again, string Id's would serve the same uniqueness purpose of the instance, right?

like image 982
gtb Avatar asked Dec 01 '13 19:12

gtb


1 Answers

This is what ES6 Map is for, it allows to use an object as key:

var map = new Map();
var obj1 = {};
var obj2 = {}; // identical, but not the same

map.set(obj1, 'value');

map.get(obj1); // 'value'
map.get(obj2); // undefined

Demo (Firefox and IE 11): http://jsbin.com/ehIgEha/1/edit?js,console, browser support: http://kangax.github.io/compat-table/es6/#Map

like image 189
Pavlo Avatar answered Sep 30 '22 20:09

Pavlo