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

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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