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

170
Vistas
Replace a variable in JSON object

I'm writing JavaScript code in which I want to replace a string in JSON object and my code is as below.

var obj = {
  "name": "name is $name",
  "work": "$name is doctor",
  "maritial status": "unmarried"
};

obj = Object.keys(obj).map(function(key, index) {
  return (obj[key].includes('$name')) ? obj[key].replace('$name', 'John'): obj[key];
});

console.log(obj);

Here I want to replace $name with John and print the JSON, but unfortunately, this is printing only the values like below instead of the whole JSON.

["name is John", "John is doctor", "unmarried"]

Where I am going wrong and how can I fix this?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You keep mentioning JSON but what you have is a JavaScript object. JSON is a string. So it would be much easier for you to keep it as a string, do a simple replace on the name, and then parse it to an object.

const json = '{"name":"name is $name","work":"$name is doctor", "maritial status":"unmarried"}';

const updated = json.replaceAll('$name', 'John');

console.log(JSON.parse(updated));

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to get a transformed object as the result, either assign the changed value to the original object:

var obj = {
  "name": "name is $name",
  "work": "$name is doctor",
  "maritial status": "unmarried"
};

Object.keys(obj).forEach(function(key) {
  if (obj[key].includes('$name')) {
    obj[key] = obj[key].replace('$name', 'John');
  }
});

console.log(obj);

Or use Object.entries and Object.fromEntries to map to a new object:

var obj = {
  "name": "name is $name",
  "work": "$name is doctor",
  "maritial status": "unmarried"
};
const newObj = Object.fromEntries(
  Object.entries(obj).map(([key, val]) => [key, val.replace('$name', 'John')])
);

console.log(newObj);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Object.entries method or other object methods to solve your problem one of them might be ...

let obj = {
  "name": "name is $name",
  "work": "$name is doctor",
  "maritial status": "unmarried"
};

Object.entries(obj).forEach(item => {
  if ((obj[item[0]].includes('$name'))) {
    item[1] = item[1].replace("$name", "John");
    obj[item[0]] = item[1];
  }
})
console.log(obj);

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