I serialize a sorted array in the background and pass it to the front desk. When transferring, the front desk automatically sorts the results according to the key value. The key value is the id value corresponding to the data. Under the premise that the key value must be brought, How to make it unsorted? Please give me more advice
The above is the correct order, and the following is the order after json.parse(). Maybe I didn’t express it well
I serialize a sorted array in the background and pass it to the front desk. When transferring, the front desk automatically sorts the results according to the key value. The key value is the id value corresponding to the data. Under the premise that the key value must be brought, How to make it unsorted? Please give me more advice
The above is the correct order, and the following is the order after json.parse(). Maybe I didn’t express it well
JSON.parse generates objects, and the keys of objects are in no order.
JavaScript objects are indeed unordered, and may appear to be sorted by key value due to HASH and other reasons.
If you return an ordered array, it should not be out of order. If it is out of order, most likely you will return a key-value pair object. Give you a test code.
<code class="javascript">// 随机生成 data let 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}` }); } // 按 text 排序 data.sort((a, b) => { let ta = a.text; let tb = b.text; return ta < tb ? -1 : (ta > tb ? 1 : 0); }); // 生成 JSON var json = JSON.stringify(data); console.log(json); // parse console.log(JSON.parse(json));</code>
I’m not familiar with PHP, I should probably handle it this way
<code class="php">$data = array(20=>'a',10=>'b'); echo json_encode($data); // {"20":"a","10":"b"} // 上面是原来的输出结果 $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"}]</code>
The JSON obtained in this way is parsed into an array, which is the original order.
JSON.parse will not cause the sequence to be messed up. I guess what you wrote to the front end is {key1: value1, key2: value2}, which is a json structure similar to Map. The front end cannot traverse such a thing, so use Object.keys() This will lead to the disorder you mentioned. It is recommended that you return a structure such as [{key: value}, {key: value}, {key: value}] which can be traversed directly.
JSON.parse My impression is to convert the "{}" string into an object
The order of using JSON.parse in the frontend of the array should not change, right? I don’t understand the meaning of the question?
If the poster wants to use for...of... at the front desk, I think there is no way to do it
Random it yourself
Convert the key value to a string and it will not be sorted.
Reference: http://php.net/manual/zh/func...
Focus on the option constant