channelList = [{ id: 0, name: '推荐' }, { id: 1, name: '区块链' }]
console.log(channelList)
// 浅拷贝
const newArr = channelList.map(obj => {
const newObj = { ...obj } //浅拷贝只拷贝一层
return newObj
})
// console.log(newArr)
newArr.forEach((obj, index) => {
delete obj.name
obj.seq = index + 1
})
// 浅拷贝后对数组的操作不会影响原数组
console.log('浅拷贝:')
console.log(newArr)
newArr[0].id = 1
console.log(channelList)
// 快速浅拷贝
console.log('快速浅拷贝:')
const newArr1=[]
Object.assign(newArr1,channelList)
newArr1[0] = { ...channelList[0] }
// const newArr1=[]
// Object.assign(newArr1,channelList)
console.log(newArr1[0])
newArr1[0].id=1
console.log(channelList)
// 直接赋值的话
const newArr2 = channelList
newArr2.forEach((obj, index) => {
delete obj.name
obj.seq = index + 1
})
console.log(newArr2)
newArr2[0] = 1
console.log(channelList)
console