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

140
Visualizações
Creating an updated and edited nested object from given one (JavaScript)

I got a schema object that looks like this:

const schema = {
social: {
    facebook: 'someValue',
    twitter: {
        department: {
            departmentImage: {
                editable: 'someValue'
            }
         }
      }
   }
};

The editable property indicates a value that I want to edit, and may appear in several nested locations in the object.

My approach to edit it is to recursively create a new object who is an exact copy of the original, and populate a new value where I encounter editable. Like this:

const formatSchema = (schema, data, formattedSchema = {}) => {
  for (const schemaKey in schema) {
    const firstKey = Object.keys(schema[schemaKey])[0];
            
    if (schema[schemaKey] instanceof Object) {
        formattedSchema[schemaKey] = schema[schemaKey];
        formatschema(schema[schemaKey], data, formattedSchema[schemaKey]);
    }
    if (schema[schemaKey] instanceof Object && firstKey === 'editable') {
       *replacing data logic*
       formattedSchema[schemaKey] = ...*replacingData*;
       formatschema(schema[schemaKey], data, formattedSchema[schemaKey]);
    } else {
        formattedSchema[schemaKey] = schema[schemaKey];
    }
  }
 return formattedSchema;
};

But I feel this solution may be inefficient as I create every single bit of the object from scratch and this would happen thousands of times a day.

Is there a way to do it better?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here's a recursive immutable update that works for any native input type. Don't worry about performance here as it's plenty fast, even if your object has thousands of fields. Let me know how this suits you and I can make a change if it's needed -

function update(t, func) {
  switch (t?.constructor) {
    case Object:
      return Object.fromEntries(
        Object.entries(t).map(([k,v]) =>
          [k, func([k, update(v, func)])]
        )
      )
    case Array:
      return t.map((v, k) => func([k, update(v, func)]))
    default:
      return func([null, t])
  }
}

const schema = {
  social: {
    facebook: 'someValue',
    twitter: {
      department: {
        departmentImage: {
            editable: 'someValue'
        }
      },
      someArr: [{ editable: 1 }, { editable: 2 }, { hello: "world" }]
    },
  }
}

console.log(update(schema, ([k,v]) =>
  k == "editable" ? "✅" : v
))
.as-console-wrapper {min-height: 100% !important; top: 0}

{
  "social": {
    "facebook": "someValue",
    "twitter": {
      "department": {
        "departmentImage": {
          "editable": "✅"
        }
      },
      "someArr": [
        {
          "editable": "✅"
        },
        {
          "editable": "✅"
        },
        {
          "hello": "world"
        }
      ]
    }
  }
}
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