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

80
Visualizações
Loop through Object and replace values based on Regex in given Array

I have an object and an array. I try to loop through object keys, check them against a regex in array and then replace the string. What I have so far:

let event = {
  "firstKey":"1",
  "secondKey": {
    "a":"one",
    "b":"two",
    "c":"three"
  },
  "id":111934444,
  "count":1,
  "location":"https://www-domain-org/username=steven&tel=1234",
  "title":"lastname=hey",
  "sd":"24-bit"
};
    
let myReg = [
  {
    name: 'telephone',
    reg: /((tel=)|(telephone=))[\d\+\s][^&\/\?]+/gi,
    group: '$1'
  },
  {
    name: 'names',
    reg: /((username=)|(lastname=))[^&\/\?]+/gi,
    group: '$1'
  }
];
    
let secret = function(obj){
  Object.values(obj).forEach(function(val) {
    myReg.forEach(function(regTest) {
      val = val.toString();
      val = val.replace(regTest.reg, regTest.group + '[redacted ' + regTest.name + ']');
    });
  });
  return obj;
}

let newEvent = secret(event);
console.log(newEvent);

I do not get the original Object with replaced values. What is wrong with my code? I appreciate any help.

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

0

You are setting the local variable val. You should actually use the obj to persist the change. Try iterating with Object.keys(obj) like this

Object.keys(obj).forEach(function(key) {
    myReg.forEach(function(regTest) {
      let val = obj[key].toString();
      obj[key] = val.replace(regTest.reg, regTest.group + '[redacted ' + regTest.name + ']');
    });
  });
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