简体   繁体   English

$.getJSON 会重新排序数据吗?

[英]Does $.getJSON reorder data?

JS: JS:

$.getJSON('services/get_locations.php', {region: $("#oblast").val()}, function(data) {
    console.log(data);
});

PHP: PHP:

$result = json_encode($raw['data']);
echo $result;
exit;

Result from var_dump($result) is: 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"
}

As you can see, first ID is 10971.如您所见,第一个 ID 是 10971。

However, the result of console.log(data) is:但是, console.log(data)的结果是:

{179: "с. АКАЦИЕВО", 919: "с. АНТИМОВО", 10971: "гр. ВИДИН"}

Why is data being reordered?为什么要重新排序数据?

Javascript objects with numeric keys will always get ordered in ascending order of the numeric key values带有数字键的 Javascript 对象将始终按数字键值的升序排列

If order is important change structure to an array something like [{id:179, value: "...."}] or [[10971,"wrd"],[179,"xyz"]]如果顺序很重要,则将结构更改为数组,例如[{id:179, value: "...."}][[10971,"wrd"],[179,"xyz"]]


Example with no ajax.没有ajax的例子。 Note the log order is different than the constructed order (ascending key values)请注意,日志顺序与构造顺序不同(键值升序)

 const data = { "10971":"\г\р. \В\И\Д\И\Н", "179":"\с. \А\К\А\Ц\И\Е\В\О", "919":"\с. \А\Н\Т\И\М\О\В\О" } console.log(data); console.log('Keys:', Object.keys(data))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM