Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda