The javascript-JSON.parse () sorts the returned array by the key value? How to make it unordered

Source: Internet
Author: User
I serialize an sorted array in the background and send it to the foreground. The foreground will automatically sort the result according to the key value during conversion. The key value is the id value corresponding to the data, how can I keep a key value unordered? Please advise if the above is the correct order. below is json. parse (...

I serialize an sorted array in the background and send it to the foreground. The foreground will automatically sort the result according to the key value during conversion. The key value is the id value corresponding to the data, how can I keep a key value unordered? Please advise


The above is the correct order. The following is the order after json. parse (). It may be that I have not expressed it very well.

Reply content:

I serialize an sorted array in the background and send it to the foreground. The foreground will automatically sort the result according to the key value during conversion. The key value is the id value corresponding to the data, how can I keep a key value unordered? Please advise


The above is the correct order. The following is the order after json. parse (). It may be that I have not expressed it very well.

JSON. parse is generated as an object, and the keys of objects are unordered.

JavaScript objects are indeed unordered and may look like they are sorted by key values for HASH and other reasons.

If you return an ordered array, it should not be in disorder. If it is out of order, most of them are a key-value pair object. Here is a piece of test code.

// Randomly generate datalet data = []; for (var I = 0; I <10; I ++) {let textOrder = ~~ (Math. random () * 10000 ). toString (); textOrder = '000 $ {textOrder} '; textOrder = textOrder. substr (textOrder. length-4); data. push ({key :~~ (Math. random () * 1000), text: 'Text $ {textOrder} '});} // sort data by text. sort (a, B) => {let ta =. text; let tb = B. text; return ta <tb? -1: (ta> tb? 1: 0);}); // Generate JSONvar json = JSON. stringify (data); console. log (json); // parseconsole. log (JSON. parse (json ));
Supplement

I am not familiar with PHP. I should probably handle it like this.

$ Data = array (20 => 'A', 10 => 'B'); echo json_encode ($ data); // {"20": "", "10": "B"} // The above is the original output result $ list = []; foreach ($ data as $ k => $ v) {array_push ($ list, array ("key" => $ k, "value" => $ v) ;}echo json_encode ($ list); [{"key": 20, "value ": "a" },{ "key": 10, "value": "B"}]

In JSON, parse is an array, which is the original order.

JSON. parse does not cause sequence disorder. I guess what you wrote to the front end is {key1: value1, key2: value2}, which is similar to the json structure of Map. such a thing cannot be traversed at the front end, and the Object is used. keys () will lead to disorder. We recommend that you return a structure like [{key: value}, {key: value}, {key: value}] that can be directly traversed.

JSON. parse I remember converting the "{}" string to an object.

The JSON. parse sequence of the array front-end should not be changed. what do I mean when I cannot solve the problem?

If the landlord wants to use for... of... on the front desk, I don't think I can do it.

Random yourself

The key value will not be sorted if it is converted to a string.

Reference: http://php.net/manual/zh/func...
Focus on option constants

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.