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

196
Views
Mapa a través de una matriz de matrices, para encontrar id y cambiar objeto

Tengo una matriz de ids ['id1', 'id3'] .

También tengo una variedad de items :

 [ { children: [{ children: [{ id: "id1", //This is value I need to find status: { state: false}, //this is value I need to change }], }], }, { children: [{ children: [{ id: "id2", status: { state: false}, }], }], }, { children: [{ children: [{ id: "id3", status: { state: false}, }], }], }, ]

Mi objetivo es encontrar cada item por id de la primera matriz y cambiar state atributo, luego devolver todos los items que incluyen los que he cambiado.

Este fue mi intento, pero devuelve todos los elementos nuevamente, tampoco estoy seguro de cómo cambiar el atributo.

 items.filter(item => item.children.map(child => child.children.map(object => idsArray.map(id => id === object.id) )))
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creo que puedes usar una función recursiva como la siguiente:

 let ids = ["id1", "id2"]; let arrayOfItems = [ { children: [ { children: [ { id: "id1", status: { state: false } } ] } ] }, { children: [ { children: [ { id: "id2", status: { state: false } } ] } ] }, { children: [ { children: [ { id: "id3", status: { state: false } } ] } ] } ]; function changeStatus(arrayOfItems, ids) { return arrayOfItems.map((e) => { if (e.id && ids.includes(e.id)) { return { ...e, status: { state: true } }; } else if (e.children) { return { ...e, children: changeStatus(e.children, ids) }; } else { return { ...e }; } }); } console.log(changeStatus(arrayOfItems,ids));

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!