0

I wrote this script,

<script>

const http = new XMLHttpRequest()
url = "http://192.168.0.2/hotspots";
window.onload = function(){
    var request = new XMLHttpRequest();

    request.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            result = eval(this.responseText)
            select = document.getElementById('access_points_select');
            for (var i = 0; i<=result.length; i++){
                var opt = document.createElement('option');
                opt.value = result[i];
                opt.innerHTML = result[i];
                select.appendChild(opt);
            }
        }
};

request.open('GET', url);
request.send();
}

</script>
</head>
<body>
<select name="access_points" id="access_points_select">
</select>

but because resource also returns duplicates i want to avoid duplicate entries into the select dropdown, how can I achieve this without creating unique array without creating another array then result.

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

1 Answers1

0

If eval(this.responseText) returns an array you can use a Set to ensure there are no duplicates.

If you need to filter undefined you can do chain that on the end too:

 result = [...new Set(this.responseText)].filter(Boolean)
Will Jenkins
  • 9,507
  • 1
  • 27
  • 46