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

200
Visualizações
Replace a special part of a JSON string

I have a JSON object called users like this:

[{"givenName":"helen","sn":"brown","phone":"44512","details":"512"},{{"givenName":"John","sn":"Doe","phone":"44600","details":"512"}]

I apply json.stringify method to convert this JavaScript object to a JSON string. Then, I use replace to replace 512 value with active. Here is my code to implement this:

var jsonUsers = JSON.stringify(users).replace(/512/g, 'active');

The problem is that when I use this code, it replaces all 512 with active (for example in my example, it changesdetails values and also it changes the phone value of the first person with something like 44active), but I want to apply this replacement only on the values of the details key, not on whole of the string (for example I don't want to check and replace the value of the phone). How can I do this?

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

0

Don't try to replace parts of a JSON. Fix it while it's an object instead.

const arr = [{"givenName":"helen","sn":"brown","phone":"44512","details":"512"},{"givenName":"John","sn":"Doe","phone":"44600","details":"512"}];

for (const item of arr) {
  if (item.details === '512') {
    item.details = 'active';
  }
}
console.log(JSON.stringify(arr));

about 4 years ago · Juan Pablo Isaza Relatório

0

const users = [{"givenName":"helen","sn":"brown","phone":"44512","details":"512"},{"givenName":"John","sn":"Doe","phone":"44600","details":"512"}]

Now use array.map()

const newArray = users.map(e => {
    return {...e, details: e.details==512 ? "active" : e.details};
});
about 4 years ago · Juan Pablo Isaza Relatório

0

If you only want to replace the text from details property then no need to convert the whole object into a string. You can simply iterate over the array using Array.map() method and then replace the details property value based on the regex.

Try this :

const jsonObj = [{
  "givenName": "helen",
  "sn": "brown",
  "phone": "44512",
  "details": "512"
}, {
  "givenName": "John",
  "sn": "Doe",
  "phone": "44600",
  "details": "512"
}];

const res = jsonObj.map(obj => {
    obj.details = obj.details.replace(/512/g, 'active');
  return obj;
});

console.log(res);

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