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

243
Visualizações
javascript, loop string xml inside for loop

hey guys I have trouble with loop string for xml inside for loop. I have tried this and the loop is not work

var data = [{"name": "Tom", age: "20"}, {"name": "Jerry", age: "20"}]

for (i = 0; i < data.length; i++) {
   var xmltext = '<Placemark>\n' + 
        '\t<name>' + data[i].name + '</name>\n' +
        '\t<age>' + data[i].age + '</age>\n' +
        '</Placemark>';
}

var filename = "file.xml";
var bb = new Blob([xmltext], {type: 'text/xml'});

document.getElementById('download').setAttribute('href', window.URL.createObjectURL(bb));
document.getElementById('download').setAttribute('download', filename);

the result I expected in xml

<Placemark>
  <name>Tom</name>
  <Age>20</Age>
</Placemark>
<Placemark>
  <name>Jerry</name>
  <Age>20</Age>
</Placemark>

is there something i'm missing or i'm doing it wrong? I wish your help

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you create the on every iteration the xmltext var new. But you want to just declerate it once and add the String to it like this:

var data = [{"name": "Tom", age: "20"}, {"name": "Jerry", age: "20"}]
var xmltext = "";
for (i = 0; i < data.length; i++) {
xmltext = xmltext + '<Placemark>\n' + 
    '\t<name>' + data[i].name + '</name>\n' +
    '\t<age>' + data[i].age + '</age>\n' +
    '</Placemark>';
}
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