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