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?