1 使用forEach()方法和delete操作符
const arr = [
{ name: 'John', age: 25, score: -10 },
{ name: 'Jane', age: 30, score: 20 },
{ name: 'Bob', age: 35, score: -5 }
];
arr.forEach(obj => {
if (obj.score > 0) {
delete obj.score;
}
});
console.log(arr);
2 使用for循环和splice()方法
const arr = [
{ name: 'John', age: 25, score: -10 },
{ name: 'Jane', age: 30, score: 20 },
{ name: 'Bob', age: 35, score: -5 }
];
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i].score > 0) {
arr.splice(i, 1);
}
}
console.log(arr);
3 结果 既是是调用方法, 整个项目的原始数据都改变