0

Lets say I have an array of zipcodes. This is just an example for you to have a clue what I am asking.

zipcodes = [53081, 53083, 54935, 53711, 54935, 53081, 57322, 53083, 53711];

How can I loop through the array only once to output only the unique zipcodes as a HTML list? (53081, 53083, 54935, 53711, 53722)

My code below opens a record set from another file without knowing how many unique zip codes there are. I have it where it outputs all of them.

I am only suppose to output the unique zip codes as a list in HTML.

function zips() {
    "use strict";

    var zipCodeRecords;
    var zipCode;
    var uniqueZips;
    var output;

    uniqueZips = "";

    // get the HTML output tag to add list
    output = document.getElementById("outputDiv");

    // open zipcode records
    zipCodeRecords = openZipCodeStudyRecordSet();

    while(zipCodeRecords.readNextRecord()) {
        zipCode = zipCodeRecords.getSampleZipCode();

        uniqueZips += "<li>" + zipCode + "</li>";
    }

    output.innerHTML = uniqueZips;

}
M.P.T.
  • 97
  • 8
  • 1
    If I'm Not Wrong, Your Answer Here: https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates. If I'm Right Then POSSIBLE DUPLICATE. – Swaggerboy Dec 18 '18 at 05:57
  • @Swaggerboy correct, very elegant solution as well – Bibberty Dec 18 '18 at 06:00
  • Possible duplicate of [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Swaggerboy Dec 18 '18 at 06:04

3 Answers3

1

The quickest way is to add each element as a key in an object.

If the same key is defined more than once, it will not increase the number of keys (no duplicates)

var zipcodes = [53081, 53083, 54935, 53711, 54935, 53081, 57322, 53083, 53711];
var uinque = {};

zipcodes.map(function(z){ uinque[z]=z; });

console.log(Object.keys(uinque));
Ahmad
  • 12,336
  • 6
  • 48
  • 88
0

You can use filter() by passing the current item, index and the array itself. Then return items only if the index of current item is equal to the current index.

var zipcodes = [53081, 53083, 54935, 53711, 54935, 53081, 57322, 53083, 53711];

var res = zipcodes.filter((v, idx, arr) => arr.indexOf(v) === idx);
console.log(res);
Giulio Bambini
  • 4,695
  • 4
  • 21
  • 36
0
function zips() {
    "use strict";

    var zipCodeRecords;
    var zipCode;
    var uniqueZips;
    var output;

    uniqueZips = "";

    // get the HTML output tag to add list
    output = document.getElementById("outputDiv");

    // open zipcode records
    zipCodeRecords = openZipCodeStudyRecordSet();

    zipcodesList = [];
    while(zipCodeRecords.readNextRecord()) {
        zipCode = zipCodeRecords.getSampleZipCode();
   if(zipcodesList.indexOf(zipCode )== -1){
        zipcodesList [] = zipCode
        uniqueZips += "<li>" + zipCode + "</li>";
      }

    }

    output.innerHTML = uniqueZips;

}

//This should help