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

203
Vistas
change value of a key in all objects of an array JAVASCRIPT

I have an array of objects that looks like this:

[
   {
        "text":"Same but with checkboxes",
        "opened": true,
        "children":[
        {
            "text":"initially selected",
            "opened":true
        },
      ]
   },
   {
        "text":"Same but with checkboxes",
        "opened":true,
        "children":[
        {
            "text":"initially open",
            "opened":true,
            "children":[
               {
                  "text":"Another node",
                  "opened":true,
               }
            ]
        },
        {
            "text":"custom icon",
            "opened":true,
        },
        {
            "text":"disabled node",
            "opened":true,
        }
      ]
    },
    {
        "text":"And wholerow selection",
        "opened":true,
    }
]

I want to know if it is possible to change the value for example of the key opened (to false) to all objects at all levels .. how can I do this?

I tried something like that without success

myArray.map(e => ({ ...e, opened: false }))
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Make a recursive function - if the object being iterated over has a children array, call it for all such children.

const input=[{text:"Same but with checkboxes",opened:!0,children:[{text:"initially selected",opened:!0}]},{text:"Same but with checkboxes",opened:!0,children:[{text:"initially open",opened:!0,children:[{text:"Another node",opened:!0}]},{text:"custom icon",opened:!0},{text:"disabled node",opened:!0}]},{text:"And wholerow selection",opened:!0}];

const closeAll = (obj) => {
  obj.opened = false;
  obj.children?.forEach(closeAll);
};
input.forEach(closeAll);
console.log(input);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Recursion is here to help. Recursively search all the objects for opened key and switch it to false.

var data = [ { "text": "Same but with checkboxes", "opened": true, "children": [ { "text": "initially selected", "opened": true }, ] }, { "text": "Same but with checkboxes", "opened": true, "children": [ { "text": "initially open", "opened": true, "children": [ { "text": "Another node", "opened": true, } ] }, { "text": "custom icon", "opened": true, }, { "text": "disabled node", "opened": true, } ] }, { "text": "And wholerow selection", "opened": true, } ];

function run(data) {
  for (let subData of data) {
    if (subData["opened"])
      subData["opened"] = false;
    if (subData["children"])
      run(subData["children"])
  }
}
run(data)
console.log(data)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just extend your map method to handle recursively. (children exist or not and Array or single object)

const updateOpened = (data) => {
  if (Array.isArray(data)) {
    return data.map(updateOpened);
  }
  const { children, ...item } = data;
  return children
    ? { ...updateOpened(item), children: updateOpened(children) }
    : { ...item, opened: true };
};

const arr=[{text:"Same but with checkboxes",opened:!0,children:[{text:"initially selected",opened:!0}]},{text:"Same but with checkboxes",opened:!0,children:[{text:"initially open",opened:!0,children:[{text:"Another node",opened:!0}]},{text:"custom icon",opened:!0},{text:"disabled node",opened:!0}]},{text:"And wholerow selection",opened:!0}];

console.log(updateOpened(arr));

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