0

I have two arrays of zip codes. The first array has many zip codes including duplicates. The second array has the duplicates removed to show only the unique zip codes.

How can I check how many times each unique zip code appears in the zip code containing duplicates?

For example, if I have two values for 21218, how can I check that there are two values? I would like to iterate through the unique name array if possible to check against the duplicate name array.

Edit: This question is similar to this previously asked question here. However, it differs because the goal is to use the existing code and incorporate it into the solution.

var url = 'https://data.baltimorecity.gov/resource/uds6-qsb6.json?$limit=50000';
var manyZipArray = [];
var zipArray = [];


$(document).ready(function() {
  $.getJSON(url, function(data) {
    for (var i = 0; i < data.length; i++) {
      manyZipArray.push(data[i].zip);
    };
    $.each(manyZipArray, function(i, el) {
      if ($.inArray(el, zipArray) === -1) zipArray.push(el);
    });
    zipArray.sort();
    for (var i = 0; i < zipArray.length; i++) {
      $('#myList').append("<li>" + zipArray[i] + "</li>");
    };
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3 style="text-align: center" ;>Unique Zip Codes </h3>
<ul id="myList"></ul>

3
  • The answers in the linked duplicate will allow you to make a map from zip code to duplicate count. Then you can just reference the count in the map as a simple object property lookup. Commented Dec 14, 2015 at 14:50
  • Its not completely a duplicate I think though, the question is more how to incorporate it handy in the existing code no? Commented Dec 14, 2015 at 14:52
  • The second answer on the link and Bas van Stein's work (tested and writing responses now). Is it a duplicate if the goal was to use existing code? Commented Dec 14, 2015 at 14:57

3 Answers 3

1

You can use a javascript object to store the number of occurences of each zip code by setting a new key of the object using the zip code.

See code below:

var url = 'https://data.baltimorecity.gov/resource/uds6-qsb6.json?$limit=50000';
var manyZipArray = [];
var zipArray = [];
var numberOfZips = {};//object to hold the counters


$(document).ready(function() {
  $.getJSON(url, function(data) {
    for (var i = 0; i < data.length; i++) {
      manyZipArray.push(data[i].zip);
      if (data[i].zip in numberOfZips){
          numberOfZips[data[i].zip] += 1;
      }else{
          numberOfZips[data[i].zip] = 1;
      }
    };
    $.each(manyZipArray, function(i, el) {
      if ($.inArray(el, zipArray) === -1) zipArray.push(el);
    });
    zipArray.sort();
    for (var i = 0; i < zipArray.length; i++) {
      $('#myList').append("<li>" + zipArray[i] +","+numberOfZips[zipArray[i]]+ "</li>");
    };
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3 style="text-align: center" ;>Unique Zip Codes </h3>
<ul id="myList"></ul>

Sign up to request clarification or add additional context in comments.

Comments

0

I would do a loop that iterates the non-duplicate array, and for each zip value, iterate the full array and count it's appearances. In my example code below I store the values in a new array, but you can do whatever you need with them.

var zipOcurrences = array();
var i = 0;
zipArray.each(function(){
  zipOcurrences[i] = 0;
  var zip = $(this);
  manyZipArray.each(function(){
    if ($(this)==zip){
      zipOcurrences[i]++;
    }
  });
  i++;
});

2 Comments

This is a very inefficient way to solve the problem.
this breaks the sort
0

You can replace the codes in your zipArray array with objects containing the zipcode and the number of occurences

var url = 'https://data.baltimorecity.gov/resource/uds6-qsb6.json?$limit=50000';
var manyZipArray = [];
var zipArray = [];

$(document).ready(function() {
  $.getJSON(url, function(data) {
    for (var i = 0; i < data.length; i++) {
      manyZipArray.push(data[i].zip);
    };
    $.each(manyZipArray, function(i, el) {
      // custom search in array
      var index = arrayObjectIndexOf(zipArray, el, "code");
      if (index === -1) {
          zipArray.push({ code: el, occur: 1 });
      }
      else {
          zipArray[index].occur++;
      }
    });
    // custom sort to sort according to 'code' attribute
    zipArray.sort(function(a, b) { 
        if (a.code< b.code) return -1;
        if (a.code > b.code) return 1;
        return 0;
    });
    for (var i = 0; i < zipArray.length; i++) {
      $('#myList').append("<li>" + zipArray[i].code + " (" + zipArray[i].occur + ")</li>");
    };
  });
});

function arrayObjectIndexOf(myArray, searchTerm, property) {
    for(var i = 0, len = myArray.length; i < len; i++) {
        if (myArray[i][property] === searchTerm) return i;
    }
    return -1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3 style="text-align: center" ;>Unique Zip Codes </h3>
<ul id="myList"></ul>

1 Comment

Snap, too slow and harder answer than @Bas van Stein

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.