简体   繁体   English

当Key是对象时,如何使用Map?

[英]How can I use Map when Key are objects?

I'm learning about javascript, but I've got a problem here. 我正在学习javascript,但我在这里遇到了问题。 I want a hashmap in JS that can have object as keys. 我想在JS中使用一个可以将对象作为键的hashmap。

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) 我需要如果我有一个与另一个对象“b”具有相同值的对象“a”,当我将“map.set”放在对象“b”上时,它应该更新对象“a”键的值,而不是在“地图”上创建一个新的键/值(在地图上复制具有相同值的两个键的类型)

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. 我这样做是因为我正在尝试创建一个QLearning表,其中每个状态都是一个对象,但我希望如果在我的代码的两个不同迭代中我得到相同的状态,Map应该引用相同的值。

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/ JS小提琴: https//jsfiddle.net/7zht40Lo/

Thanks in advance! 提前致谢!

The problem here is, that the 2 Objects you have are not the same. 这里的问题是,你拥有的2个对象是不一样的。 so a !== b . 所以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 你可以做这样的事情 :(也就是说,制作元素ab的所有属性的唯一标识符

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. 有两种结构:“{first:1,second:2}”,每种结构都存储为不同的对象。 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 计算机盲目地将每个对象写入内存并将地址返回到分别存储在内存中的“a”和“b”变量。

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. 现在,当您引用“a”时,计算机只会将地址引用放在那里。

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 ...因为“a”和“b”指向记忆中的不同“事物”

What you'll need to do is set "b" to "a" as: 您需要做的是将“b”设置为“a”,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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