js实现列表修改
修改数组元素
使用索引直接赋值修改数组中的元素:
let arr = [1, 2, 3];
arr[1] = 4; // 修改第二个元素
console.log(arr); // [1, 4, 3]
使用splice方法
splice方法可以删除、替换或添加元素:
let fruits = ['apple', 'banana', 'cherry'];
// 从索引1开始删除1个元素并插入'orange'
fruits.splice(1, 1, 'orange');
console.log(fruits); // ['apple', 'orange', 'cherry']
使用map创建新数组
通过map方法返回新数组而不修改原数组:
let numbers = [1, 2, 3];
let doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
修改对象数组属性
当数组元素是对象时,可以修改对象属性:
let users = [{name: 'Alice'}, {name: 'Bob'}];
users[1].name = 'Charlie';
console.log(users); // [{name: 'Alice'}, {name: 'Charlie'}]
使用fill方法批量修改
fill方法用静态值填充数组中指定范围的元素:
let arr = [1, 2, 3, 4];
arr.fill(0, 1, 3); // 从索引1到3填充0
console.log(arr); // [1, 0, 0, 4]
使用扩展运算符修改
结合扩展运算符和slice方法实现非破坏性修改:

let colors = ['red', 'green', 'blue'];
let newColors = [...colors.slice(0, 1), 'yellow', ...colors.slice(2)];
console.log(newColors); // ['red', 'yellow', 'blue']






