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

161
Visualizações
json.stringify object and rename variables in json

I want to serialize an object o, which has a method called, let's say, a. The object also holds a variable, which name is _a.

I now want to parse this object to a JSON string. But the JSON looks something like this:

{
    "_a": "",
    ...
}

Question

Is there a way, to comfortably remove/ replace the _ character(s) (or any character(s)).

What I have tried

  1. The rename parameter of the JSON.stringify() method.
    1.1. Didn't work, because you can only return altered values and no keys.
  2. Iterating all keys of an object, deleting them and creating a new renamed key, and assigning the value (see code below).
    2.1. This works, but is not really readable and nasty, when having "sub-object".
Object.keys(o).forEach(key => {
    Object.defineProperty(o, key.replace("_", ""),
        Object.getOwnPropertyDescriptor(o, key));
    delete o[key];
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Would something like this work for you:

let obj = {
  "_a": "",
  "b": "Test"
}

let result = Object.entries(obj).reduce((acc, [key, value]) => {
  let newKey = key.replace('_', '')
  acc[newKey] = value
  return acc
}, {})

console.log(result)

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