简体   繁体   English

节点JS-对象值更新异常行为

[英]Node JS - Object Value Update Strange Behavior

I have a simple object I'm trying to modify and the results are puzzling. 我有一个要修改的简单对象,结果令人费解。 This is executed in node.js. 这是在node.js中执行的。

I have an object as follows: 我有一个对象,如下所示:

var element = {  
  ident: "value",
  green:
    { date: value2,
      key: value3,
      key2: value4,
      key3: 
       { id1: 
         { p1: [],
           p2: [],
           p3: [],
           p4: [] },
       { id2: 
         { p1: [],
           p2: [],
           p3: [],
           p4: [] },
       { id3: 
         { p1: [],
           p2: [],
           p3: [],
           p4: [] } } },
  red:
    { date: value5,
      key: value6,
      key2: value7,
      key3: 
       { id1: 
         { p1: [],
           p2: [],
           p3: [],
           p4: [] },
       { id2: 
         { p1: [],
           p2: [],
           p3: [],
           p4: [] },
       { id3: 
         { p1: [],
           p2: [],
           p3: [],
           p4: [] } } } };

When I try to set the value of a single "p2" to an array i've filled with some data, each "p2" for that color is set, not just the one referenced. 当我尝试将单个“ p2”的值设置为已填充一些数据的数组时,将设置该颜色的每个“ p2”,而不仅仅是引用的那个。 I'm setting the value like so: 我正在这样设置值:

element[green].key3[id1][p2] = someArray; element [green] .key3 [id1] [p2] = someArray;

everything in brackets are variables pointing to the key name. 方括号中的所有内容都是指向键名的变量。 The output of the above sets 3 values... all the ids of Green - green.key3.id1.p2, .id2.p2 and .id3.p2 all to the value I only want for green.key3.id1.p2 上面的输出将3个值... Green-green.key3.id1.p2,.id2.p2和.id3.p2的所有id都设置为我只想要green.key3.id1.p2的值

Any ideas as to what might be happening here? 关于这里可能发生什么的任何想法? Thanks in advance for the help. 先谢谢您的帮助。

How are you building this object? 您如何构建此对象? It may be that you've assigned the same empty array to every p2 in a colour. 可能是您已经为每种颜色的p2分配了相同的空数组。 When you do that each p2 will be a reference to the same array, so pushing a value into one will apparently push that value into each. 当您这样做时,每个p2将是对同一数组的引用,因此将一个值压入一个值显然会将那个值压入每个数组。 To test if these are references to the same array, check: 要测试这些是否是对同一数组的引用,请检查:

green.key3.id1.p2 === red.key3.id2.p2

if this gives you true , then what I said above is the case. 如果这让你true ,那就是我上面说的话。 Essentially this is like: 本质上是这样的:

var a = [];
var b = a;
var c = a;

b.push(1); // b = [1], c = [1];
b === c; // true, since they are really references to the same object (array).

EDIT 编辑

After rereading, it may be that actually id1 , id2 etc. are assigned to the same object. 重新读取后,可能实际上是将id1id2等分配给了同一对象。 ie

var id = { p1: [], p2: [], p3: [] };
green.key3.id1 = id;
green.key3.id2 = id;

Like the above, you can test if this is the case by doing: 像上面一样,您可以通过以下方法测试是否是这种情况:

// true if these are really references to the same object.
green.key3.id1 === green.key3.id2;

If you want a quick way to reuse code without this behavior, then you can do: 如果您想要一种快速的方法来重用没有这种行为的代码,则可以执行以下操作:

function Id() {
    this.p1 = [];
    this.p2 = [];
    this.p3 = [];
}

green.key3.id1 = new Id();
green.key3.id2 = new Id();

Each new Id() will be a new object. 每个new Id()将是一个新对象。

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

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