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

170
Visualizações
Replace specific character from an Array of objects with another character in JS

I'm trying to replace character "/" with a blank space or a dash "-", but i can't seem to figure out how. I've tried some of the answers i've found here but it returns the same string. I have two set of arrays.

set A: elemnts = ["String1", "String2", "String3/"] set B gets each character of a s string from A to evaluate, an replace in case "/" is found.

 for(var i=0; i< elements.length; i++){
          arrayV = elements[i].value;
             for (var j=0; j <= arrayV.length; j++) {

                if(arrayV.charAt(j) === '/'){
arrayV[j] = "-";
                 }
   
    
                }
    
                elements[i]= arrayV;
       }

This is how i solved it (i learned something today):

var elements = document.getElementsByName("inputCont");

for(var i=0; i< elements.length; i++){

      var rString = elements[i].value;
  
         rString = rString.replace('/','-');
        
         elements[i].value = rString;
   }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In this example I use the function map() to create a new array and replace() to replace the character.

var elemnts = ["String1", "String2", "String3/"];

var result = elemnts.map(str => str.replace('/','-'));

console.log(result);

Update

OK, I missed the part about selecting the input elements. You can "cast" elements as an array and then iterate over that using map():

var elements = document.getElementsByName("inputCont");

var result = [...elements].map(elm => elm.value.replace('/', '-'));

console.log(result);
<form name="form01">
  <input type="text" name="inputCont" value="String" />
  <input type="text" name="inputCont" value="String" />
  <input type="text" name="inputCont" value="String/" />
</form>

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