How to iterate a url in Javascript

Asked

Viewed 29 times

0

I have a tag img and I need to iterate the src, altering within a map to uri.

ex: ../assets/images/personagens/${index}.jpg, whose index is the parameter of the map, however the javascript does not allow this action within a require(url).

Does anyone have any idea?

  • What have you tried?

  • @Matheus Maricondi you care to show what you have tried, maybe this can help you better understand your question. Thank you

1 answer

1

I don’t quite understand your purpose.

Example:

var indexes = [1, 2, 3, 4];

function myFunction() {
  x = document.getElementById("images")
  x.innerHTML = indexes.map((currentValue, index) => {
    var uri = '../assets/images/personagens/${currentValue}.jpg'
    return `<img src="{uri}">`
  })
}
<!DOCTYPE html>
<html>
<body>

<p>Clique no Botão para gerar imagens</p>

<button onclick="myFunction()">Criar Imagens</button>

<div id="images"></div>

</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.