亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - Is there any way to sort objects in js?
僅有的幸福
僅有的幸福 2017-06-29 10:09:03
0
10
1187

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?

僅有的幸福
僅有的幸福

reply all(10)
為情所困
ss.sort(function(a, b){
    return a.UserID > b.UserID;
});
phpcn_u1582
var obj = {
    1 :[{userID:'1'}],
    2 :[{userID:'2'}],
    H :[{userID:'3'}],
    Z :[{userID:'4'}],
};
var objKeys = Object.keys(obj);
objKeys.sort((a,b) =>{
    return a>b;
}).map((val) => {
    console.log("userID "+ obj[val][0].userID)
});
/ 輸出
// userID 1
// userID 2
// userID 3
// userID 4
巴扎黑

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.

In

ES6, Object.getOwnPropertyNames() and Object.getOwnPropertySymbols() and Reflect.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.

phpcn_u1582

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.

世界只因有你
// 對(duì)array 排序
 res.sort((a, b) => {
   if (a.UserID > b.UserID ) {  // 可以改成其他key
      return 1
   } else {
      return -1
   }
  })
過去多啦不再A夢(mèng)

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

代言
Object.keys(objs).sort()可以獲取到排好序的keys
var objs = {
    f: {
        id: 2,
        name: '2'
    }, 
    a: {
        id: 3,
        name: '3'
    }, 
    c: {
        id: 1,
        name: '1'
    }
};
// 自定義排序規(guī)則,按對(duì)象的id排序
var sortedObjKeys = Object.keys(objs).sort(function(a, b) {
    return objs[b].id - objs[a].id;
});

// 按默認(rèn)排序規(guī)則,按對(duì)象的key排序
var sortedObjKeys = Object.keys(objs).sort();

for (var index in sortedObjKeys) {
    console.log(sortedObjKeys[index]);
    console.log(objs[sortedObjKeys[index]]);
    console.log('----------');
}
過去多啦不再A夢(mèng)

If you want to have sequential values, save them in an array, and then put them in the json attributes.

小葫蘆
Object.keys(obj).sort(function() {
    // 為了以防萬一,這里先排好鍵值順序,代碼省略,也可以直接用sort()默認(rèn)排序
}).map(function(v) {
    return obj[v]; // 根據(jù)原鍵名從obj中再找對(duì)應(yīng)的項(xiàng)
});

In this way, an array is returned, which is in a fixed order.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template