replace text in an array with the map function

1

I do not understand why I can not replace the text but I can show it

<p id="poesia">
    LA SCARPETTA
    Mi sa che ieri sera eri di fretta
    guarda che ti sei persa la scarpetta.
    io stamattina l’ho trovata
    con la carrozza ieri sei scappata.
    E’ stato bello insieme a te danzare
    il Cuore tu mi hai fatto sussultare,
    </p>
    <button onclick="iterare()">iterar</button>
    <script type="text/javascript">
      texto = document.getElementById('poesia').textContent
      
      lineas = texto.split(/\n/) // division x saltos de lineas
      
      function iterare(){
        lineas.map(function(elemento, indice){
          if(indice % 2 == 0){
            console.log(elemento)
            /*
            "Mi sa che ieri sera eri di fretta"
     "io stamattina l’ho trovata"
    "E’ stato bello insieme a te danzare"
            */
            lineas[indice].replace(elemento, 'lineas')
          }
        })
      }
      //undefined
    </script>
    
asked by steven 27.04.2017 в 23:13
source

3 answers

2

the function .replace , does not act on the String, but returns a String with the result of the replacement, so what you should do is the following:

lineas[indice] = lineas[indice].replace(elemento, 'lineas')

Here I'll give you the example and how to use W3School

I also leave you with here the MSDN documentation

    
answered by 27.04.2017 в 23:36
2

When you execute the replace, it effectively replaces one text with the other, but you do not do anything with it, in this example you concatenate it in a new text, with which you fill the element with the id 'poetry'

function iterare(){
    
    texto = document.getElementById('poesia').textContent
    lineas = texto.split(/\n/) // division x saltos de lineas
    res=""
    lineas.map(function(elemento, indice){
      if(indice % 2 == 0){
        console.log(elemento)
        res+=lineas[indice].replace(elemento, " Lineas ")
      }else{
        res+=lineas[indice]
      }
    })
    console.log(res)
    document.getElementById('poesia').innerHTML=res
  }
  //undefined
<p id="poesia">
LA SCARPETTA
Mi sa che ieri sera eri di fretta
guarda che ti sei persa la scarpetta.
io stamattina l’ho trovata
con la carrozza ieri sei scappata.
E’ stato bello insieme a te danzare
il Cuore tu mi hai fatto sussultare,
</p>
<button onclick="iterare()">iterar</button>
    
answered by 27.04.2017 в 23:34
2

The function call replace that returns a String more is not assigned to the index and current element of map within the array, to solve this, you must assign the index of the form.

lineas[indice] = lineas[indice].replace(elemento, 'lineas');

What he raises may happen if another arrangement is returned.

texto = document.getElementById('poesia').textContent
  lineas = texto.split(/\n/) // division x saltos de lineas
  function iterare(){
    var neww = lineas.map(function(elemento, indice){
      if(indice % 2 == 0) return lineas[indice].replace(elemento, 'lineas');
      else return lineas[indice];
    })
    console.log(neww);
  }
<p id="poesia">
LA SCARPETTA
Mi sa che ieri sera eri di fretta
guarda che ti sei persa la scarpetta.
io stamattina l’ho trovata
con la carrozza ieri sei scappata.
E’ stato bello insieme a te danzare
il Cuore tu mi hai fatto sussultare,
</p>
<button onclick="iterare()">iterar</button>
    
answered by 27.04.2017 в 23:42