gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:27:12 24 4
gpt4 key购买 nike

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

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

我通过将 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 对象。所有键(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)

关于JavaScript 对象操作在唯一操作后显示不同键的相同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com