How to avoid repeated data in datalist from xml

1

So far I have managed to get what I need from the xml list, but the issue is that I have twice the center and two times Alameda stay AlamedaCentroCentroAlameda, someone to help me with how to make it go out just once?

             <!DOCTYPE HTML>
 <html>
<head>
<meta charset="utf-8">
<script type="text/javascript" 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>

<script>
$(document).ready(function() { //se empieza a ejecutar cuando el DOM esté listo

               $.get("marcadores.xml", {}, function(xml) { //Abrimos el archivo marcadores.xml

                //El ciclo se va repetir cada vez que se encuentre la etiqueta Usuario

                $('Usuario', xml).each(function() {
                    var Sector = $(this).find('Sector').text();
                    console.log(Sector);
                     $('<option>'+Sector+'</option>').appendTo('#Lista')
                    console.log(Lista)
                    })
                    })
                    })
  </script>
 </head>
<body>
 <datalist id="Lista"></datalist>  
  <input  type="text" name="Texto" list="Lista">
     </body>
      </html>
    
asked by Luis 18.01.2018 в 22:11
source

1 answer

3

You can include the added values in an array to avoid repeating them:

$(document).ready(function() { //se empieza a ejecutar cuando el DOM esté listo
  $.get("marcadores.xml", {}, function(xml) { //Abrimos el archivo marcadores.xml
    //El ciclo se va repetir cada vez que se encuentre la etiqueta Usuario
    var valores = [];
    $('Usuario', xml).each(function() {
      var Sector = $(this).find('Sector').text();
      if (valores.indexOf(Sector) < 0){
        $('<option>'+Sector+'</option>').appendTo('#Lista');
        valores.push(Sector);
      }
    });
  });
});
    
answered by 18.01.2018 / 22:21
source