问题如下:

遇到的问题,数组monthArr1 push进了多个obj,但是 在修改某个num值时,导致别的num值也发生了变化。
而这就是深拷贝浅拷贝的问题。

解决浅拷贝使用深拷贝最简单方法 :JSON.parse(JSON.stringify(obj))
或者:
使用深拷贝函数来进行深拷贝。
function deepCopy(obj) { if (typeof obj !== 'object' || obj === null) { return obj; } let copy; if (Array.isArray(obj)) { copy = []; for (let i = 0; i < obj.length; i++) { copy[i] = deepCopy(obj[i]); } } else { copy = {}; for (let key in obj) { if (obj.hasOwnProperty(key)) { copy[key] = deepCopy(obj[key]); } } } return copy; } const obj = { month: monthStart.toISOString().slice(0, 7), num: 0 }; const copiedObj = deepCopy(obj); this.monArr.push(copiedObj);
function deepCopy(obj) {
  if (typeof obj !== 'object' || obj === null) {
    return obj;
  }
  let copy;
  if (Array.isArray(obj)) {
    copy = [];
    for (let i = 0; i < obj.length; i++) {
      copy[i] = deepCopy(obj[i]);
    }
  } else {
    copy = {};
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        copy[key] = deepCopy(obj[key]);
      }
    }
  }
  return copy;
}
const obj = {
  month: monthStart.toISOString().slice(0, 7),
  num: 0
};
const copiedObj = deepCopy(obj);
this.monArr.push(copiedObj); 
                

















