js怎么实现删除
删除数组元素的方法
使用 splice() 方法可以从数组中删除指定位置的元素。该方法会修改原数组,并返回被删除的元素组成的数组。
const arr = [1, 2, 3, 4, 5];
arr.splice(2, 1); // 从索引2开始删除1个元素
console.log(arr); // [1, 2, 4, 5]
删除对象属性的方法
使用 delete 操作符可以删除对象的某个属性。删除后该属性将不再存在于对象中。

const obj = { a: 1, b: 2, c: 3 };
delete obj.b;
console.log(obj); // { a: 1, c: 3 }
删除DOM节点的方法
使用 removeChild() 或 remove() 方法可以从DOM树中删除节点。
// 方法1:使用removeChild
const parent = document.getElementById('parent');
const child = document.getElementById('child');
parent.removeChild(child);
// 方法2:使用remove
const element = document.getElementById('element');
element.remove();
删除字符串中字符的方法
虽然字符串是不可变的,但可以通过替换或截取的方式实现删除效果。

const str = 'hello world';
const newStr = str.replace('o', ''); // 删除第一个'o'
const newStr2 = str.slice(0, 5) + str.slice(6); // 删除索引5的字符
删除Map或Set中的元素
对于Map和Set数据结构,有专门的删除方法。
// Map删除
const map = new Map();
map.set('key', 'value');
map.delete('key');
// Set删除
const set = new Set([1, 2, 3]);
set.delete(2);
删除LocalStorage或SessionStorage
使用 removeItem() 方法可以删除Web Storage中的特定项。
localStorage.removeItem('key');
sessionStorage.removeItem('key');






