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

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()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.

        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.

          世界只因有你
          // 对array 排序 res.sort((a, b) => { if (a.UserID > b.UserID ) { // 可以改成其他key return 1 } else { return -1 } })
            过去多啦不再A梦

            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' } }; // 自定义排序规则,按对象的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('----------'); }
                  过去多啦不再A梦

                  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()默认排序 }).map(function(v) { return obj[v]; // 根据原键名从obj中再找对应的项 });

                    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
                      About us Disclaimer Sitemap
                      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!