JavaScript对象操作在唯一操作后显示不同键的相同值

JavaScript对象操作在唯一操作后显示不同键的相同值,javascript,node.js,object,javascript-objects,key-value,Javascript,Node.js,Object,Javascript Objects,Key Value,我正在使用Node.js后端,它为相同的日期键提取不同的值。我有下面的代码,在其中我声明了一个months对象,它存储默认的日期列表(为简单起见缩短) 然后将这些值指定给不同的键,作为要返回的最终数据对象的模板 我通过将numIncrease值增加1模拟了数据操作。所需输出为: { '2019-01-01': 6 } { '2019-01-01': 7 } { '2019-01-01': 8 } { '2019-01-01': 9 } 但当我运行它时,我会得到以下代码: data: { key

我正在使用Node.js后端,它为相同的日期键提取不同的值。我有下面的代码,在其中我声明了一个months对象,它存储默认的日期列表(为简单起见缩短)

然后将这些值指定给不同的键,作为要返回的最终数据对象的模板

我通过将numIncrease值增加1模拟了数据操作。所需输出为:

{ '2019-01-01': 6 }
{ '2019-01-01': 7 }
{ '2019-01-01': 8 }
{ '2019-01-01': 9 }
但当我运行它时,我会得到以下代码:

data: { key1: { '2019-01-01': 15 },
  key2: { '2019-01-01': 15 },
  key3: { '2019-01-01': 15 },
  key4: { '2019-01-01': 15 } }
此外,numIncrease并不是每次只增加1。除非我遗漏了一个明显的范围错误,否则我听说JavaScript在幕后的工作方式有些“奇怪”的地方。我还在学习,所以这是一次相当不错的学习经历

下面的示例已准备好运行。谢谢你的帮助

let months = {
    '2019-01-01': 5,
}

// key data key is assigned to months for template use
let data = {
    key1: months,
    key2: months,
    key3: months,
    key4: months,
}

// Initial Num Increase
let numIncrease = 1

/**
 * Key 1
 */
for (let date in data.key1) {
    data.key1[date] += numIncrease
}
numIncrease += 1; // increase num to simulate different manipulation on next key
console.log(data.key1) // key1: { '2019-01-01': 6 }

/**
 * Key 2
 */
for (let date in data.key2) {
    data.key2[date] += numIncrease
}
numIncrease += 1; // increase num to simulate different manipulation on next key
console.log(data.key2) // key2: { '2019-01-01': 7 }

/**
 * Key 3
 */
for (let date in data.key3) {
    data.key3[date] += numIncrease
}
numIncrease += 1; // increase num to simulate different manipulation on next key
console.log(data.key3) // key1: { '2019-01-01': 8 }

/**
 * Key 4
 */
for (let date in data.key4) {
    data.key4[date] += numIncrease
}
numIncrease += 1; // increase num to simulate different manipulation on next key
console.log(data.key4) // key4: { '2019-01-01': 9 }

// For some reason all data key nums now equal the last iteration through where key4 = 15
console.log("data: ", data)

在代码中,内存中只有一个
months
对象。所有键(键1、键2、键3和键4)都指向此月对象。因此,当您遍历代码时,您正在操作内存中的同一个month对象。换句话说,它们必须相同,因为只有一个
months
对象

如果您希望它们是不同的对象,请考虑在分配给所有键时创建副本。下面的示例使用spread操作符来执行此操作

let months={
'2019-01-01': 5,
}
//密钥数据密钥已分配给月份以供模板使用
让数据={
键1:{…月数},
键2:{…月数},
键3:{…月数},
键4:{…月数}
}
//初始数量增加
设numIncrease=1
/**
*关键1
*/
for(输入数据中的日期。键1){
data.key1[date]+=numIncrease
}
numIncrease+=1;//增加num以模拟下一个关键点上的不同操作
console.log(data.key1)//key1:{'2019-01-01':6}
/**
*关键2
*/
for(输入数据中的日期。键2){
data.key2[date]+=numIncrease
}
numIncrease+=1;//增加num以模拟下一个关键点上的不同操作
console.log(data.key2)//key2:{'2019-01-01':7}
/**
*关键3
*/
for(输入数据中的日期。键3){
data.key3[date]+=numIncrease
}
numIncrease+=1;//增加num以模拟下一个关键点上的不同操作
console.log(data.key3)//key1:{'2019-01-01':8}
/**
*关键4
*/
for(输入数据中的日期。键4){
data.key4[date]+=numIncrease
}
numIncrease+=1;//增加num以模拟下一个关键点上的不同操作
console.log(data.key4)//key4:{'2019-01-01':9}
//出于某种原因,所有数据键num现在都等于最后一次迭代,其中key4=15
console.log(数据)