我有以下JS从外部JSON文件(下面)读取并循环遍历所有返回的学校,然后使用if语句将通过表单提交的zip代码与每个JSON对象的zip属性进行比较。Javascript JSON结果排序
我期望的匹配将按照他们在JSON文件中遇到的顺序返回,但是当我搜索zip 76248时,我在控制台中获得下面的输出。
$("#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
我想保持这些在JSON文件中遇到我将使用返回的输出在另一个函数的顺序。
问题是:为什么129不是返回的第一个ID?
你在运行什么浏览器?问题是,在Javascript中没有保证对象的属性是按照从JSON解析时被放入或遇到的顺序读取的。大多数浏览器保留属性(或密钥)顺序,但例如某些版本的Chrome不支持, – Antares42
我在Chrome中工作,但我在IE11和Firefox中检查过它,结果相同。 –
是的,当我运行''Object.keys(学校)'(或只是输入“学校”到Firebug中)我得到的键/属性为104,123,129 ......我猜测浏览器正在做内部诉诸和索引,这是优化的按键检索值。不幸的是,这只是它,jQuery或任何其他库都不能改变它。 – Antares42