Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!