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

119
Vistas
forEach a deeplist and tree to change the value of a key

I have a deep list and tree in which I want to change all the toggle keys to true, but, because of the structure it's hard to accomplish.

I wrote this function but it's useless I need one with forEach and it doesn't work

function loopThruAforest() {
    return state.data.map((tree) => {
        console.log(tree) && loopThruATree(tree)
     
    });
    function loopThruATree(tree) {
      return tree.children.map((node) => {
        if (node.children !== null) {
          return  console.log(tree) && loopThruATree(tree)
        } else {
          return node.datum;
        }
      });
    }
  }
{
  "data": [{
      "datum": "String",
      "id": 1,
      "toggle": false,
      "children": [{
          "datum": "String",
          "id": 2,
          "toggle": false,
          "children": [{
            "datum": "String",
            "id": 3,
            "toggle": false,
            "children": []
          }]
        },
        {
          "datum": "String",
          "id": 4,
          "toggle": false,
          "children": []
        }
      ]
    },
    {
      "datum": "String",
      "id": 5,
      "toggle": false,
      "children": [{
        "datum": "String",
        "id": 6,
        "children": []
      }]
    }
  ]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can try to have a to_visit variable to make a DFS:

function loopThruAforest(data){
    let to_visit = [...data];
    while(to_visit.length > 0){
        let current = to_visit.pop()
        current.toggle = true;
        to_visit.concat( current.children)
    }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Object.keys to iterate objects and arrays. (But don't forget that you don't want to iterate strings.)

function setTogglesTo(data, bool) {
  if (!data) return

  for (const key of Object.keys(data)) {
    if (key === "toggle")
      data[key] = bool

    if (typeof data[key] != "string")
      setTogglesTo(data[key], bool)
  }
}

setTogglesTo(dataObj, true)
console.log(dataObj)
<script>
  const dataObj = [{
      "datum": "String",
      "id": 1,
      "toggle": false,
      "children": [{
          "datum": "String",
          "id": 2,
          "toggle": false,
          "children": [{
            "datum": "String",
            "id": 3,
            "toggle": false,
            "children": []
          }]
        },
        {
          "datum": "String",
          "id": 4,
          "toggle": false,
          "children": []
        }
      ]
    },
    {
      "datum": "String",
      "id": 5,
      "toggle": false,
      "children": [{
        "datum": "String",
        "id": 6,
        "children": []
      }]
    }
  ];
</script>

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