A json object is returned in the background, and the order is already arranged
But when I use js to for in to traverse this object like traversing an array, the result is different from the original object. After checking the information, I found out that the js object is unordered. . So is there a way to traverse this object sequentially? Or how to get the properties of the original object in order?
No solution. If sorted, you should use an array. Or with an array of keys.
The order in objects is not specified in ES5, so different engines may be different.
InES6,
Object.getOwnPropertyNames()
andObject.getOwnPropertySymbols()
andReflect.ownKeys()
which is equivalent to the combination of the two will output in a certain order, but it is not the answer you want.It seems that the structure of json is arranged in the order of numbers and dictionaries. If this is the case, you can sort it manually.
1. I feel that if your page display is exactly in the sorting order returned by the backend, then you don’t need to sort, just display it directly.
2. If the desired order is different from the back-end order, then it depends on which fields the products are sorted and displayed. Then you sort based on this field in the object.
You should be able to get what you want.
Because json objects have no order, "pre-arranged order" does not actually exist.
If the front end wants to sort based on key names, it can first take out the key names, sort them, and then get the content.
Since the background returns sorted data, if you use ajax to request data, dataType: json, after you receive the data, just traverse it directly and fill it in the template. If the order is wrong, I feel it’s because the data you got from the background is wrong
If you want to have sequential values, save them in an array, and then put them in the json attributes.
In this way, an array is returned, which is in a fixed order.