Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
Map through array of arrays, to find id and change object

I have an array of ids ['id1', 'id3'].

I also have an array of 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}, 
                            }],
                  }],
      },
    ]

My goal is to find every item by id from first array, and change attribute state, then return all items having included those I have changed.

This was my try, but it returns all items again, also Im not sure how to change the attribute.

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 Respuestas
Responde la pregunta

0

I think you can use recursive function something like below :

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda