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

212
Visualizações
cambiar el valor de una clave en todos los objetos de una matriz JAVASCRIPT

Tengo una matriz de objetos que se ve así:

 [ { "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, } ]

Quiero saber si es posible cambiar el valor por ejemplo de la llave abierta (a falso) a todos los objetos en todos los niveles.. ¿cómo puedo hacer esto?

Intenté algo así sin éxito.

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

0

Cree una función recursiva: si el objeto que se itera tiene una matriz de children , llámelo para todos esos niños.

 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 Relatório

0

La recursividad está aquí para ayudar. Busque recursivamente todos los objetos para la clave opened y cámbielo a falso.

 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 Relatório

0

Simplemente extienda su método de mapa para manejar recursivamente. (los niños existen o no y Array u objeto único)

 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 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