简体   繁体   English

JavaScript关联数组的行为异常

[英]JavaScript associative array behaving strangely

I have two objects. 我有两个对象。 When I pass these as key to an associative array, and assign a value, all the values are stored wrong ( except for the last one ). 当我将这些作为键传递给关联数组并分配一个值时,所有值都存储错误(最后一个值除外)。

Can anyone please tell me what I'm doing wrong ?? 谁能告诉我我做错了吗?

 var usrMrid = {name: "mrid"}; var usrXYZZ = {name: "xyzz"}; var comm = {}; comm[usrMrid] = "ONE"; comm[usrXYZZ] = "TWO"; console.log("usrMrid: " + comm[usrMrid]); // this gives TWO, when it should give ONE console.log("usrXYZZ: " + comm[usrXYZZ]); // this works fine 

When you use [] syntax with object and you pass an object as a property name, property name is going to be the string representation of the given expression, which will be in your case [object Object] . 当对对象使用[]语法并将对象作为属性名称传递时,属性名称将是给定表达式的string表示形式,在您的情况下将是[object Object] So when you use different objects, they create the same property with name [object Object] and override the previous ones. 因此,当您使用不同的对象时,它们将创建名称为[object Object]的相同属性,并覆盖先前的属性。

You can see it in the example. 您可以在示例中看到它。 Here I print the properties of the object and you can see that there is just one property with name [object Object] . 在这里,我打印了对象的属性,您可以看到只有一个名称为[object Object]属性。

 var usrMrid = {name: "mrid"}; var usrXYZZ = {name: "xyzz"}; var comm = {}; comm[usrMrid] = "ONE"; comm[usrXYZZ] = "TWO"; console.log(comm); 

You can use Map for that case 您可以在这种情况下使用Map

 var usrMrid = {name: "mrid"}; var usrXYZZ = {name: "xyzz"}; var comm = new Map([ [usrMrid, "ONE"], [usrXYZZ, "TWO"] ]); console.log(comm.get(usrMrid)); 

Object keys are String only! 对象键仅是字符串! Use a Map for that: 为此使用地图:

var usrMrid = {name: "mrid"};
var usrXYZZ = {name: "xyzz"};

const comm = new Map([
  [usrMrid,"ONE"],
  [usrXYZZ, "TWO"]
]);

console.log(comm.get(usrMrid));

However this is a really good usecase for Symbols too: 但是,这对于Symbols也是一个非常好的用例:

 const id = Symbol("id");

var usrMrid = {
  name: "mrid",
  [id]:"ONE"
};
var usrXYZZ = {
   name: "xyzz",
    [id]:"TWO"
};

 console.log(usrMrid, usrMrid[id]);

well this has a problem, when you write an object in associative array style it only accepts strings. 好吧,这是有问题的,当您以关联数组样式编写对象时,它仅接受字符串。 In your case it will overwrite itself and will always have the same answer for both the objects, 在您的情况下,它将覆盖自己,并且对两个对象始终具有相同的答案,

you need to write something like this otherwise it will always overwrite it. 您需要编写类似这样的内容,否则它将始终覆盖它。

var usrMrid = {name: "mrid"};
var usrXYZZ = {name: "xyzz"};

var comm = {};

comm[usrMrid.name] = "ONE";
comm[usrXYZZ.name] = "TWO";

console.log(comm);// this will print your object correctly.
console.log(comm[usrMrid.name]);// this will print ONE
console.log(comm["mrid"]);// this will also print ONE
console.log(comm["xyzz"]); // This will print TWO
console.log(comm[usrXYZZ.name]); // This will print TWO

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

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