EDIT: one part of my function was missing.
It's my first time posting here so let me know if I'm doing it wrong.
Here is my problem:
My data is like this:
const lists = [
{
idl: 'todo-0',
namel: 'Example 1',
tasks: [{ taskName: 'task', completedTask: true, taskId: "Un" },{ taskName: 'task2', completedTask: true, taskId: "Deux" },{ taskName: 'task3', completedTask: true, taskId: "Trois" }]
},
{
idl: 'todo-1',
namel: 'Example 2',
tasks: [{ taskName: 'task', completedTask: true, taskId: "Un" },{ taskName: 'task2', completedTask: true, taskId: "Deux" },{ taskName: 'task3', completedTask: true, taskId: "Trois" }]
}]
I tried a lot of thing and VScode get angry because of my synthax. I can aim the 2 array I want with this function but it remove the keys of tasks :<
const toggleTask = (listId: string, taskId: string) => {
const listIndex = lists.findIndex((list)=>list.idl === listId);
const list = lists[listIndex]
const newList = {
...list,
tasks: list.tasks.map((task)=> {if (taskId=== task.taskId){return task.completedTask === true} } )
}
const newLists = [
...lists.slice(0,listIndex),
newList,
...lists.slice(listIndex + 1),
]
console.log('newLists2', newLists)
}
What I would like is for example:
toggleTask("todo-1", "Un")
exepected output:
[
{
idl: 'todo-0',
namel: 'Example 1',
tasks: [{ taskName: 'task', completedTask: true, taskId: "Un" },{ taskName: 'task2', completedTask: true, taskId: "Deux" },{ taskName: 'task3', completedTask: true, taskId: "Trois" }]
},
{
idl: 'todo-1',
namel: 'Example 2',
tasks: [{ taskName: 'task', completedTask: false, taskId: "Un" },{ taskName: 'task2', completedTask: true, taskId: "Deux" },{ taskName: 'task3', completedTask: true, taskId: "Trois" }]
}]
Check if it helps
const toggleTask = (listId, taskId) => {
const newLists = lists.map(listItem => {
if(listItem.idl === listId){
listItem.tasks.map(task => {
if(task.taskId === taskId) {
task.completedTask = !task.completedTask;
}
return task;
});
}
return listItem;
});
console.log(newLists);
};