How to sort a JS object of objects?

后端 未结 5 1824
礼貌的吻别
礼貌的吻别 2020-12-18 03:03

I have built an object in PHP, used JSON_encode function and send it as a JSON string to my JS script via ajax. Then I convert it back to an object. The problem I am havin

相关标签:
5条回答
  • 2020-12-18 03:38
    sortObject(object){
        if(typeof object === 'object'){
            if(object instanceof Date){
                return object;
            }
            if(object instanceof Array){
                return object.map(element => this.sortObject(element));
            } else {
                return Object.keys(object).sort().reduce((result, key) => {
                    if(object[key] && object[key] !== null) {
                        result[key] = this.sortObject(object[key]);
                    }
                    return result;
                }, {});
            }
        }
        return object;
    }
    
    0 讨论(0)
  • 2020-12-18 03:43

    Here's a generic iteration function which pushes all objects into an array and sorts them by their customer property in a case-insensitive manner, then iterates over the sorted array:

    function iterate(data) {
      var arr = [];
      for (var prop in data) {
        if (data.hasOwnProperty(prop)) {
          var obj = {};
          obj[prop] = data[prop];
          obj.tempSortName = data[prop].customer.toLowerCase();
          arr.push(obj);
        }
      }
      arr.sort(function(a, b) {
        var at = a.tempSortName,
            bt = b.tempSortName;
        return at > bt ? 1 : ( at < bt ? -1 : 0 );
      });
    
      for (var i = 0, l = arr.length; i < l; i++) {
        var obj = arr[i];
        delete obj.tempSortName;
        console.log(obj);
        for (var prop in obj) {
          if (obj.hasOwnProperty(prop)) {
            var id = prop; //gets the obj "index" (id?)
          }
        }
        console.log(id);
        var item = obj[id];
        console.log(item.customer);
        //do stuff with item
      }
    }
    

    Fiddle

    0 讨论(0)
  • 2020-12-18 03:55

    It's probably the difference between a JavaScript Object and a JavaScript Array. Objects are more like hash tables, where the keys aren't sorted in any particular order, whereas Arrays are linear collections of values.

    In your back end, make sure you're encoding an array, rather than an object. Check the final encoded JSON, and if your collection of objects is surrounded by {} instead of [], it's being encoded as an object instead of an array.

    You may run into a problem since it looks like you're trying to access the objects by an ID number, and that's the index you want those objects to occupy in the final array, which presents another problem, because you probably don't want an array with 40,000 entries when you're only storing a small amount of values.

    If you just want to iterate through the objects, you should make sure you're encoding an array instead of an object. If you want to access the objects by specific ID, you'll probably have to sort the objects client-side (i.e. have the object from the JSON response, and then create another array and sort those objects into it, so you can have the sorted objects and still be able to access them by id).

    You can find efficient sorting algorithms (or use the one below from ELCas) easily via Google.

    0 讨论(0)
  • 2020-12-18 03:56

    Just cycle through your data and sort it. I'm not using the most efficient sorter but the ideas is there.

    var data = toArray(AJAX_Returned_JSON); // function to convert object to array
    var sortedData = null;
    
    for(var i=0; i<data.length - 1; i++){
      var temp = data[i];
      for(var j=i+1; j<data.length; j++){
        if(data[j].customer < temp.customer){
          temp = data[j];
        }
      }
    
      sortedData.push(temp);
    }
    
    0 讨论(0)
  • 2020-12-18 04:03

    I've changed Fabricio Matée answer to become more flexible and return the sorted object.

    function alphabetical_sort_object_of_objects(data, attr) {
        var arr = [];
        for (var prop in data) {
            if (data.hasOwnProperty(prop)) {
                var obj = {};
                obj[prop] = data[prop];
                obj.tempSortName = data[prop][attr].toLowerCase();
                arr.push(obj);
            }
        }
    
        arr.sort(function(a, b) {
            var at = a.tempSortName,
                bt = b.tempSortName;
            return at > bt ? 1 : ( at < bt ? -1 : 0 );
        });
    
        var result = [];
        for (var i=0, l=arr.length; i<l; i++) {
            var obj = arr[i];
            delete obj.tempSortName;
            for (var prop in obj) {
                if (obj.hasOwnProperty(prop)) {
                    var id = prop;
                }
            }
            var item = obj[id];
            result.push(item);
        }
        return result;
    }
    

    Then just call the function like this

    your_object = alphabetical_sort_object_of_objects(your_object, 'attribute_to_sort');
    
    0 讨论(0)
提交回复
热议问题