请原谅标题,我不知道如何描述这一点。我有一个问题,我的代码更改了变量,但没有业务更改,我不知道为什么。编码:getCalendar (parameters, callback) { var returnData = this.calendars // If limit parameter is set, remove all but n events if (parameters.limit) { returnData.forEach((calendar) => { calendar.events = calendar.events.slice(0, parameters.limit) }) } callback(returnData) }this.calendars 填充了如下所示的数据。该代码应该返回 this.calendars 的副本,事件数量受 parameters.limit 限制。这确实按预期工作,但此代码也会以某种方式从 this.calendars 中删除事件。因此,如果我在将 parameters.limit 设置为 3 的情况下运行代码,然后再次将 parameters.limit 设置为 5,它将两次返回 3 个事件。如果重要的话,这是一个节点应用程序。这个.日历[ { id: '5e29843erkjfassfv1h8@group.calendar.google.com', lastUpdate: '2019-09-18T10:16:32+02:00', summary: 'Calendar1', description: 'Description', timeZone: 'Europe/Oslo', updated: '2019-09-11T00:09:34.954Z', events: [ [Object] ] }, { id: 'd3llqgg43irfkcf8pjfe8@group.calendar.google.com', lastUpdate: '2019-09-18T10:16:32+02:00', summary: 'Calendar 2', description: 'Description', timeZone: 'Europe/Oslo', updated: '2019-09-16T16:36:41.373Z', events: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ] }]
1 回答

慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
您正在创建的浅表副本this.calendars,所以当你改变returnData它发生变异this.calendars也。尝试这个。
var returnData = JSON.parse(JSON.stringify(this.calendars))
添加回答
举报
0/150
提交
取消