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

147
Views
How to add more items in array multidimensional in React

Hi everyone before too late and hours, i can't work .

I need update each checkBox, when this is true or false (tasks array are checkBox in front End)

Data seeds
 const datas = [
    {
        id: 10,
        name: job01,
        tasks: [
            { name: "morning", value: false },
            { name: "afternoon", value: true },
            { name: "sunset", value: false },
            { name: "night", value: true },
        ],
        completed: false
    }];

the next code to update data, when press one checkbox is:

const [todos, setTodos] = useState(datas)

const toggleTask = taskId => {

    const updateTodos = todos.forEach(to => {
        to.tasks.forEach(task => {
            if (taskId === to.name) 
               console.log('update datas: ', { ...task,  value: !task.value})

            }
          })
        
    })console.log('Result: ', updateTodos ) };//only show undefined

Thnz for readme.

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

0

forEach does not return anything, you might want to first map the array, and then filter the elements:

// get a list of the tasks that you need to update
const needUpdateTodos = todos.flatMap(
     to => to.tasks.filter(task => taskId === to.name)
);
// for each task that you need to update, change the value to the opposite
const updatedTodos = needUpdateTodos.map(el => {
    task.value = !task.value;
    console.log('update datas: ', task)
    return task
})
// show resulting tasks updated
console.log('Result: ', updatedTodos )
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!