森欄
2023-03-03 10:34:46
記者:$.getJSON('services/get_locations.php', {region: $("#oblast").val()}, function(data) { console.log(data);});PHP:$result = json_encode($raw['data']);echo $result;exit;結(jié)果來(lái)自var_dump($result):{ "10971":"\u0433\u0440. \u0412\u0418\u0414\u0418\u041d", "179":"\u0441. \u0410\u041a\u0410\u0426\u0418\u0415\u0412\u041e", "919":"\u0441. \u0410\u041d\u0422\u0418\u041c\u041e\u0412\u041e"}如您所見(jiàn),第一個(gè) ID 是 10971。然而,結(jié)果console.log(data)是:{179: "с. АКАЦИЕВО", 919: "с. АНТИМОВО", 10971: "гр. ВИДИН"}為什么要重新排序數(shù)據(jù)?
1 回答

PIPIONE
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
帶有數(shù)字鍵的 Javascript 對(duì)象將始終按數(shù)字鍵值的升序排列
如果順序很重要,則將結(jié)構(gòu)更改為數(shù)組之類(lèi)的東西[{id:179, value: "...."}]或[[10971,"wrd"],[179,"xyz"]]
沒(méi)有ajax的例子。請(qǐng)注意日志順序與構(gòu)造順序不同(升序鍵值)
const data = {
"10971":"\u0433\u0440. \u0412\u0418\u0414\u0418\u041d",
"179":"\u0441. \u0410\u041a\u0410\u0426\u0418\u0415\u0412\u041e",
"919":"\u0441. \u0410\u041d\u0422\u0418\u041c\u041e\u0412\u041e"
}
console.log(data);
console.log('Keys:', Object.keys(data))
添加回答
舉報(bào)
0/150
提交
取消