Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Set a boolean to true/false by selecting 2 IDs in arrays

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" }]
    }]
    

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!