JavaScript 对象操作在唯一操作后显示不同键的相同值
JavaScript Object Manipulation showing identical values for different keys after unique manipulation
我正在开发一个 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。除非我遗漏了一个明显的范围错误,否则我听说有一些 "weird" 的东西与 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
对象。您的所有键(key1、key2、key3 和 key4)都指向这个月对象。因此,当您遍历代码时,您正在操作内存中的同一个月份对象。换句话说,它们必须相同,因为只有一个months
对象!
如果您希望它们成为不同的对象,请考虑在分配给所有键时创建一个副本。以下示例使用扩展运算符来执行此操作。
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)
我正在开发一个 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。除非我遗漏了一个明显的范围错误,否则我听说有一些 "weird" 的东西与 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
对象。您的所有键(key1、key2、key3 和 key4)都指向这个月对象。因此,当您遍历代码时,您正在操作内存中的同一个月份对象。换句话说,它们必须相同,因为只有一个months
对象!
如果您希望它们成为不同的对象,请考虑在分配给所有键时创建一个副本。以下示例使用扩展运算符来执行此操作。
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)