1. map
MDN: map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
const arr = [
{ id: 1, text: "a", completed: false },
{ id: 2, text: "b", completed: false }
];
const newArr = arr.map(item =>
item.id === 2 ? { ...item, completed: !item.completed } : item
);
console.log(newArr);
newArr 打印结果
[
{ id: 1, text: "a", completed: false },
{ id: 2, text: "b", completed: true }
]