0

I have the following JS which reads from an external JSON file (below) and loops through all returned schools, then compares the zip code submitted via form to the zip property of each JSON Object using the if statement.

I would expect that the matches would be returned in the order they are encountered in the JSON file but when I search for zip 76248, I get the below output in the console.

 $("#searchSubmit").click(function (evt) {
      evt.preventDefault();
      var searchTerm = $("#searchTerm").val();

      if (searchTerm == '' || searchTerm == null) { 
         alert ("Please enter a zip code"); 
         $("#searchTerm").focus();
         return;
      }

      if ( searchTerm.substring(0, 1) != 7) {
         alert("Please enter a Keller ISD Zip Code");
          $("#searchTerm").focus();
          return;
      }
           
      grabSchools(searchTerm);

   });

function grabSchools(zip) {
      var output = "";

       jQuery.each(schools, function (i, v) {

              if (v.zip == zip) {
               
               output += v.id + "\n";

              }

       });

       console.log(output);

    }

 var schools = {

   "129":
       {
         "id":"129",
         "name": "Ridgeview Elementary",
         "address": "1601 Marshall Ridge Pkwy.",
         "zip": "76248",
         "phone": "817-744-6600",
         "web": "",
         "map": "",
         "intermediate": "123|104",
         "middle" : "045",
         "high": "001|005"
      },
       "104":
      {
         "id":"104",
         "name": "Bear Creek Intermediate",
         "address": "801 Bear Creek Pkwy.",
         "zip": "76248",
         "phone": "817-744-3500",
         "web": "",
         "map": "",
         "middle" : "041",
         "high": "001"
      },
      "123":
      {
         "id":"123",
         "name": "Trinity Meadows Intermediate",
         "address": "3500 Keller-Hicks Road",
         "zip": "76244",
         "phone": "817-744-4300",
         "web": "",
         "map": "",
         "middle" : "045",
         "high": "005"
      },
        "042":
      {
         "id":"042",
         "name": "Fossil Hill Middle",
         "address": "3821 Staghorn Circle S.",
         "zip": "76137",
         "phone": "817-744-3050",
         "web": "",
         "map": "",
         "high": "002"
      },

      "043":
      {
         "id":"043",
         "name": "Hillwood Middle",
         "address": "8250 Parkwood Hill Blvd.",
         "zip": "76137",
         "phone": "817-744-3350",
         "web": "",
         "map": "",
         "high": "002|004"
      },

      "044":
      {
         "id":"044",
         "name": "Indian Springs Middle",
         "address": "305 Bursey Road",
         "zip": "76248",
         "phone": "817-744-3200",
         "web": "",
         "map": "",
         "high": "001|004"
      },

      "041":
      {
         "id":"041",
         "name": "Keller Middle",
         "address": "300 College Avenue",
         "zip": "76248",
         "phone": "817-744-2900",
         "web": "",
         "map": "",
         "high": "001"
      },

      "046":
      {
         "id":"046",
         "name": "Timberview Middle",
         "address": "10300 Old Denton Road",
         "zip": "76244",
         "phone": "817-744-2600",
         "web": "",
         "map": "",
         "high": "005"
      },

      "045":
      {
         "id":"045",
         "name": "Trinity Springs Middle",
         "address": "3550 Keller-Hicks Road",
         "zip": "76244",
         "phone": "817-744-3500",
         "web": "",
         "map": "",
         "high": "005"
      },
   
   "004":     
      {
         "id":"004",
         "name": "Central High",
         "address": "9450 Ray White",
         "zip": "76244",
         "phone": "817-744-2000",
         "web": "",
         "map": ""
      },
    
     "002":
      {
         "id":"002",
         "name": "Fossil Ridge",
         "address": "4101 Thompson Road",
         "zip": "76244",
         "phone": "817-744-1700",
         "web": "",
         "map": ""
      },

       "001":
       {
         "id":"001",
         "name": "Keller High",
         "address": "601 N. Pate-Orr Road",
         "zip": "76248",
         "phone": "817-744-1400",
         "web": "",
         "map": ""
      },

      "005":
       {
         "id":"005",
         "name": "Timber Creek High",
         "address": "12350 Timberland Blvd.",
         "zip": "76244",
         "phone": "817-744-2300",
         "web": "",
         "map": ""
      },

    
     


      
   
};

104
129
044
041
001

I am trying to keep these in the order encountered in the JSON file as I will use the returned output in another function.

Question is: Why isn't 129 the first ID returned?

  • What browser are you running in? The thing is, there is no guarantee in Javascript that the properties of an object are read in the order they are put in or encountered while parsing from JSON. Most browser preserve the property (or key) order, but e.g. some versions of Chrome don't, – Antares42 Feb 01 '16 at 20:16
  • I was working in Chrome, but I have checked it in IE11 and Firefox and the result was the same. – Keller_District Feb 01 '16 at 20:29
  • Yeah, when I run ``Object.keys(schools)`` (or just type "schools" into Firebug) I get the keys / properties as 104, 123, 129,... I'm guessing the browsers are doing an internal resorting and indexing that's optimized for retrieving values by key. That's unfortunately just what it is, and neither jQuery or any other library can change it. – Antares42 Feb 01 '16 at 20:58
  • It is always a good practice in javascript that whenever you want to compare thingsyou should use `===` instead of `==`. – Kunal Gadhia Feb 02 '16 at 13:07

2 Answers2

0

The short answer is: Javascript does not guarantee an ordering of keys/properties in its objects.

See here.

Community
  • 1
  • 1
Antares42
  • 1,406
  • 1
  • 15
  • 45
0

You were all on the right track, it turns out that the issue was because I was providing defined keys to make my life easier in other functions and the JS was sorting it based on that association rather than the order that it was encountered in the JSON.

When I numbered the index values in ascending order, then it sorted the way I wanted to.