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

104
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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