search

Home  >  Q&A  >  body text

javascript - Is there any way to sort objects in js?

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?

仅有的幸福仅有的幸福2712 days ago1050

reply all(10)I'll reply

  • 为情所困

    为情所困2017-06-29 10:11:03

    ss.sort(function(a, b){
        return a.UserID > b.UserID;
    });

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-29 10:11:03

    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

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-29 10:11:03

    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.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-29 10:11:03

    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.

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-29 10:11:03

    // 对array 排序
     res.sort((a, b) => {
       if (a.UserID > b.UserID ) {  // 可以改成其他key
          return 1
       } else {
          return -1
       }
      })
    

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-29 10:11:03

    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.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-29 10:11:03

    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

    reply
    0
  • 代言

    代言2017-06-29 10:11:03

    Object.keys(objs).sort()可以获取到排好序的keys
    var objs = {
        f: {
            id: 2,
            name: '2'
        }, 
        a: {
            id: 3,
            name: '3'
        }, 
        c: {
            id: 1,
            name: '1'
        }
    };
    // 自定义排序规则,按对象的id排序
    var sortedObjKeys = Object.keys(objs).sort(function(a, b) {
        return objs[b].id - objs[a].id;
    });
    
    // 按默认排序规则,按对象的key排序
    var sortedObjKeys = Object.keys(objs).sort();
    
    for (var index in sortedObjKeys) {
        console.log(sortedObjKeys[index]);
        console.log(objs[sortedObjKeys[index]]);
        console.log('----------');
    }

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-29 10:11:03

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

    reply
    0
  • 高洛峰

    高洛峰2017-06-29 10:11:03

    Object.keys(obj).sort(function() {
        // 为了以防万一,这里先排好键值顺序,代码省略,也可以直接用sort()默认排序
    }).map(function(v) {
        return obj[v]; // 根据原键名从obj中再找对应的项
    });

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

    reply
    0
  • Cancelreply