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

120
Visualizações
Is there a way to update a deeply nested value of this structure via recursive method

I am attempting to update this array of objects based on the object key. Each key maps to a registry which holds a method to format the data and return it and then update the object key. This recursive function works but cant handle an object key which has a nested object.

 [
  {
    "myData": "test",
    "nestedArr": [
      {
        "hello": "test",
        "check": "test",
        "nestedObject": {
          "hello": "hello",
          "nestedArr": [
            {
              "test": {
                "value": "updateMe"
              }
            }
          ]
        }
      }
    ]
  }
]

Here is my current implementation

const parse = async (arr) => {
  return await Promise.all(
    arr.map(async (obj) => {
      await Promise.all(
        Object.keys(obj).map(async (value) => {
          if (Array.isArray(obj[value])) {
            await parse(obj[value]);
          }

          try {
            const method = registry[value];
            const data = await method(obj, value);
            obj[value] = data;
          } catch (error) {}
        })
      );

      return obj;
    })
  );
};

UPDATED:

const parse = async (arr) => {
  return await Promise.all(
    arr.map(async (obj) => {
      await Promise.all(
        Object.keys(obj).map(async (value) => {
          if (Array.isArray(obj[value])) {
            await parse(obj[value]);
          }

          try {
            if (typeof obj[value] === "object") {
              await parse([obj[value]]);
            }
          } catch (error) {}

          try {
            const method = registry[value];
            const data = await method(obj, value);
            obj[value] = data;
          } catch (error) {}
          
        })

      );

      return obj;
    })
  );
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

@Barmar pointed me in the right direction, updated the result. Works great not too concerned on the performance aspect as this result will be cached in redis

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