简体   繁体   English

JavaScript 映射对象不保存对象键?

[英]JavaScript map object not saving object keys?

So I'm trying to add an entry to a map object with the key being an object but it doesn't seem to work.所以我试图向地图对象添加一个条目,键是一个对象,但它似乎不起作用。

let myMap = new Map();
myMap.set({type : 'sum', val : 10},1);
console.log(myMap.get({type:'sum',val:10})); <---- this returns undefined

why is it returning undefined instead of 1?为什么它返回 undefined 而不是 1?

The reason is that objects are references to values in JavaScript.原因是对象是对 JavaScript 中值的引用。 So two objects, even with the identical value are evaluated as different, since its stored as two different values.因此,即使具有相同值的两个对象也被评估为不同的,因为它存储为两个不同的值。 If you use the same, you get the result you expect:如果你使用相同的,你会得到你期望的结果:

let myMap = new Map();
const key = {type : 'sum', val : 10}
myMap.set(key, 1);
console.log(myMap.get(key)); // 1

javascriot uses SameValueZero algorithm to campare key when you set and get from this structure. javascriot 使用 SameValueZero 算法在您设置和从此结构获取时保存密钥。 SameValueZero is similar to strict equality (===). SameValueZero 类似于严格相等 (===)。 since in get method you are creating new object and pass it to this method as key this key is not strictly equal to the object you use when you set so you get null.因为在 get 方法中,您正在创建新对象并将其作为键传递给该方法,因此该键并不严格等于您在设置时使用的对象,因此您将获得空值。

It should accept key value pair only, so your are providing object in place of key.它应该只接受键值对,所以你提供对象代替键。

 myMap.set({type : 'sum', val : 10},1);
 myMap.set( {key:1, val: {type : 'sum', val : 10}} );

Try using a key and adding the object { type:'',val:''} to it as its value.尝试使用一个键并将对象 { type:'',val:''} 添加到它作为它的值。 Try wrapping '1' into the object itself尝试将 '1' 包装到对象本身中

As you will need a key to access it.因为您将需要一个密钥来访问它。

Ref: [ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map][1]参考:[ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map][1]

It will return myMap.size它将返回myMap.size

Even if your two objects used as a key looks equals (because they have the same attributes), they aren't (according to === which is more or less the operation used - see here for more details ).即使用作键的两个对象看起来相等(因为它们具有相同的属性),它们也不是(根据===或多或少使用的操作 - 请参阅此处了解更多详细信息)。

It will work if you do the following:如果您执行以下操作,它将起作用:

let myMap = new Map();
var obj = {type : 'sum', val : 10};
myMap.set(obj,1);
console.log(myMap.get(obj));

Map is a key value pair where you store a value with it's key. Map 是一个键值对,你用它的键存储一个值。 Like:喜欢:

let map = new Map();
         // key     // value
map.set('myKey','just a text');

then to access that value, you need the key in this case which is 'myKey'然后要访问该值,在这种情况下您需要密钥“myKey”

map.get('myKey'); // will return just a text

if you wish to convert your object to a map, follow this article :如果您希望将对象转换为地图,请按照这篇文章进行操作

let obj = {
  name: "John",
  age: 30
};

let map = new Map(Object.entries(obj));

alert( map.get('name') ); // John

You use 2 different objects that just happen to have the same properties.您使用 2 个不同的对象,它们恰好具有相同的属性。 They are not equal, though.但是,它们并不相等。 To make it work, you would have to use the exact same object, like this:要使其工作,您必须使用完全相同的对象,如下所示:

let myMap = new Map();
var obj = {type : 'sum', val : 10};
myMap.set(obj,1);
console.log(myMap.get(obj));

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

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