mock array object data
let newArr = [{title:'小明', id:18},{title:'小红', id:16}]
Method 1: (for loop) delete an attribute in the array object, such as deleting the id attribute
for (const key in newArr) {
// 删除id属性
delete newArr[key].id;
}
Method 2: (map loop) delete an attribute in the array object, such as deleting the id attribute
let newArrVal = JSON.parse(JSON.stringify(newArr)) //数组是引用类型, 深拷贝一下
newArrVal.map(e => { delete e.id }) //然后删除属性id
Wrong way:
let newArrVal = (JSON.parse(JSON.stringify(newArr))).map(e => { delete e.id})
//想代码一步到位, 结果得到错误的值
Because map will not change the original array, and needs to return the return value to the new array to receive!
In the above error code, there is no return return value, and newArrVal finally gets undefined.
Method 3: (forEach loop) delete an attribute in the array object, such as deleting the id attribute
newArr.forEach(item => {
if (item.id) {
// 删除id属性
delete item.id;
}
})