Take the id and mark the item is the items list above as done:true Note: You should not mutate the array | JavaScript, Node.js, React.js and Angular.js Forum
J
JYOTI SHARMA Posted on 07/01/2022
const items = [
{
id: 1,
task: "task1",
done: false
},
{
id: 2,
task: "task2",
done: false
},
{
id: 3,
task: "task3",
done: false
},
]
 
constmarkDone=(id)=>{

 

}

Y
Yogesh Chawla Replied on 10/01/2022

Mutating the array means changing the original array. Now without changing the original array, we can do this:

items.forEach( item => item.done = true );	
console.log(items);

or

var arr_copy = JSON.parse(JSON.stringify(items));
arr_copy.forEach( item => item.done = true );	
console.log(arr_copy);
console.log(items);

forEach does not mutate the original array.

or use map

let items2 = items.map(a => {return {...a}})
items2.find(a => a.id == 2).task = "modified";

items2.forEach( item => item.done = true );	
console.log(items2);
console.log(items);