Problem with "replace" in arrays [closed]

0

Good morning, I am trying to use "replace" in this that I show acontinuation:

var inconvenientes1 = [ 'BACA', 'LOCA', 'BUEY', 'COLA' ];
var inconvenientes2 = [ 'BACX', 'LOCX', 'BUEX', 'COLX' ];

for(var i = 0; i<inconvenientes1.length;i++){
  var result = document.getElementById("resultado").innerHTML = custom;
  var res = result.replace(inconvenientes1[i],inconvenientes2[i]);
  document.getElementById("resultado").innerHTML = res;
}

The problem is that it does not replace words, which can be wrong?

    
asked by A.Rafa 19.12.2017 в 05:25
source

3 answers

3

I guess that #resultado contains a phrase that in between has the words of inconvenientes1 . If so, the correct way to make the replacement would be:

var inconvenientes1 = [ 'BACA', 'LOCA', 'BUEY', 'COLA' ];
var inconvenientes2 = [ 'BACX', 'LOCX', 'BUEX', 'COLX' ];

for(var i = 0; i<inconvenientes1.length;i++){
  var result = document.getElementById("resultado").innerHTML;
  var res = result.replace(inconvenientes1[i],inconvenientes2[i]);
  document.getElementById("resultado").innerHTML = res;
}
<div id="resultado">Hay una BACA que está LOCA Y al BUEY le mordió la COLA</div>

But there is a shorter way:

var inconvenientes1 = [ 'BACA', 'LOCA', 'BUEY', 'COLA' ],
    inconvenientes2 = [ 'BACX', 'LOCX', 'BUEX', 'COLX' ],
    resultado = document.getElementById("resultado");

inconvenientes1.forEach(function(inconveniente,i) {
   resultado.innerHTML = resultado.innerHTML.replace(inconveniente,inconvenientes2[i]);
});
<div id="resultado">Hay una BACA que está LOCA Y al BUEY le mordió la COLA</div>

By the way, the CRAZY BACA should be CRAZY COW, but for purposes of being an example, we will assume that it is a very special breed of cow.

    
answered by 19.12.2017 / 12:32
source
0

As you said @ azeós in your comment, the error that can be seen in the small piece of code you have shared is that you associate custom a result. At the moment we do not know what the custom variable is, but what if I can tell you that the instructions that are executed are:

  • document.getElementById ("result"). innerHTML = custom
  • result = document.getElementById ("result"). innerHTML;
  • Therefore, you are storing in "result" the content of "custom", which has nothing to do with the HTML content that has the element with id "result" of the page. Surely this will be the main problem, and usually happens by copying and pasting without being very careful, it has all happened to us. It is a matter of time before you have the ability to see it quickly, and that these problems do not affect you at all.

    In short, you would have to modify the line ...

    var result = document.getElementById("resultado").innerHTML = custom;
    To leave it like this ...

    var result = document.getElementById("resultado").innerHTML;

    So that you can see a clearer example of what I am saying, here is an example where a multiple value assignment is made.

    var objetoCustom = "PrimerMensaje"; // Este objeto no se sabe si está inicializado siquiera
    var objetoHtml = "htmlInterno"; // Esto reemplazaría el código de document.getElementById("resultado").innerHTML
    
    var result = objetoHtml = objetoCustom;
    alert(result);
        
    answered by 19.12.2017 в 11:01
    0

    Good friend, I hope this is what you are looking for.

    First you have to have a text to be able to do the replace the idea is to make the replace to the variable custom that as initial value we add inconvenientes1[i] then replace the word with var res = custom2.replace(inconvenientes1[i],inconvenientes2[i]) but look carefully that we replace looking for the word in costom and then if we add to the html greetings I hope it helps you.

    Functional Example

    var inconvenientes1 = [ 'BACA', 'LOCA', 'BUEY', 'COLA' ];
    var inconvenientes2 = [ 'BACX', 'LOCX', 'BUEX', 'COLX' ];
    
    for(var i = 0; i<inconvenientes1.length;i++){
      //Agregando solamente el texto sin cambiar la palabra 
      var custom = "hola eso es una "+inconvenientes1[i]+", "
      document.getElementById("resultado").append(custom)
      
      //Primero cambiamos el valor del texto por el deseado y despues es que se lo asignamos para agregarlo
      var custom2 = "hola eso es una "+inconvenientes1[i]+", "
      var res = custom2.replace(inconvenientes1[i],inconvenientes2[i]);
      document.getElementById("resultado2").append(res)
    }
    #resultado {
    
      background: rgb(252, 88, 88);
      padding: 10px;
    }
    #resultado2 {
    
      background: rgb(97, 252, 88);
      padding: 10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="resultado">
    
    </div>
    <br>
    <div id="resultado2">
    
    </div>
        
    answered by 19.12.2017 в 15:07