简体   繁体   中英

How can I use Map when Key are objects?

I'm learning about javascript, but I've got a problem here. I want a hashmap in JS that can have object as keys.

I need that if I have an object "a" with the same values as another object "b", when I put "map.set" on object "b", it should update the values from object "a" key, and not create a new key/values on "map" (kind of duplicating two keys with the same values on Map)

I'm doing this because I'm trying to create a QLearning table, where each state will be an object, but I want that if in two different iterations of my code I get the same state, the Map should refer to the same values.

Example below:

var QTable = new Map();
var a = {first: 1, second: 2};
var b = {first: 1, second: 2};

QTable.set(a, [0,0,0,0,0,0]);
QTable.set(b, [1,1,1,1,1,1]);

// I would expect to have an Object with a single item, and the values updated to [1,1,1,1,1,1] instead of two objects with the same key-values.

console.log(QTable);

JS Fiddle: https://jsfiddle.net/7zht40Lo/

Thanks in advance!

The problem here is, that the 2 Objects you have are not the same. so a !== b . Thats what gives you two different keys.

You could do something like this tho: (That is, making an Unique Identifier of all properties of your elements a and b

var QTable = new Map();
var a = {first: 1, second: 2};
var b = {first: 1, second: 2};

QTable.set("first"+a.first+"second"+a.second+"", [0,0,0,0,0,0]);
QTable.set("first"+b.first+"second"+b.second+"", [1,1,1,1,1,1]);

console.log(QTable);

NOTE: this can be optimized. I just wanted to give you an idea of how to do what you want.

There are two structures: "{first: 1, second: 2}" and each are stored as distinct objects. They are not recognized as "the same thing" to the computer -- even though you, a tricky human, can make that assumption.

The computer blindly wrote each object into memory and returned the address to where they were stored in memory to the "a" and "b" variables respectively

var QTable = new Map();
var a = {first: 1, second: 2}; // a now points to address EF75DC3D
var b = {first: 1, second: 2}; // b now points to address 2CBC2EF7

Now, when you refer to "a", the computer will just put the address reference there.

So this:

QTable.set(a, [0,0,0,0,0,0]); 
QTable.set(b, [1,1,1,1,1,1]);

... as recognized as this:

QTable.set(EF75DC3D, [0,0,0,0,0,0]); 
QTable.set(2CBC2EF7, [1,1,1,1,1,1]);

... because "a" and "b" are pointing to different "things" in memory

What you'll need to do is set "b" to "a" as:

var QTable = new Map();
var a = {first: 1, second: 2}; // a now points to address EF75DC3D
var b = a;                     // b is now like an alias to "a"
                               // and will ultimately point to 
                               // "a's" address  EF75DC3D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM