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

105
Visualizações
Convert object to string and fill NA in case value is not present javascript

I am trying to convert a string into an object and fill NA in case the value for that key is not present.

I have a string like this.

 let stroy =  {"Name":"","Id":"abc",  "test": "" "}

with this

JSON.Parse(stroy)

I am able to convert , but How to fill NA in case no value is present.

Is there any other way to do this ?

Thanks

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

0

You can loop the object after parsing. While looping just check whether data found in that key or not

let stroy =  `{"Name":"","Id":"abc",  "test": ""}`
stroy = JSON.parse(stroy)
for (var key in stroy) {
  if(!stroy[key]) {
    stroy[key] = 'NA'
  }
}
console.log(stroy)

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use Object.keys() and Array.map() to create a list of key / values pairs. If the value is empty we'll replace it with a default value, in this case 'NA'.

We're use Object.fromEntries() to convert back to an object.

let stroy = {"Name":"", "Id":"abc", "test": ""};
const defaultValue = 'NA';

let result = Object.fromEntries(Object.keys(stroy).map(key => {
    return [key, stroy[key] || defaultValue ];
}));

console.log('Result:', result);

about 4 years ago · Juan Pablo Isaza Relatório

0

You have a extra " in your object it seems.

let stroy =  {"Name":"","Id":"abc",  "test": ""}
let stringStroy = JSON.stringify(stroy).replaceAll('""','"NA"')
console.log(JSON.parse(stringStroy))
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