2 回答

TA貢獻1828條經(jīng)驗 獲得超3個贊
擴展運算符創(chuàng)建淺表副本。如果菜單的內(nèi)容是對象,則更改復制數(shù)組中的這些對象將更改原始數(shù)組中的這些對象(或者從技術上講,這兩個引用針對同一對象):
const obj1 = {
val: 1
}
const obj2 = {
val: 2
}
const obj3 = {
val: 3
}
const arr = [obj1, obj2, obj3]
// creating a copy with the spread operator
const copy = [...arr]
// changing the second element in the copy
copy[1].val = 22
// the element in the original array is changed too
console.log(arr)

TA貢獻1851條經(jīng)驗 獲得超4個贊
我通過以下方式進行了深度克隆
const cloned = menu.getMenu().map(x => Object.assign({}, x));
? ? console.log('cloned ', cloned)
? ? this.menuItems = this.permissionService.filterMenuByTopic(cloned);
添加回答
舉報