0

I have a question about sorting a dictionary object is JS es6. I am trying to filter the array but still keep the structure of an array in each month's object. Here is a mock of the data:

"soldListings": {
    "Jan - 2020": [
        {
            "id": 159,
            "user_id": 1,
            "type_id": 1,
        },
        {
            "id": 173,
            "user_id": 1,
            "type_id": 1,
        },
        {
            "id": 563,
            "user_id": 1,
            "type_id": 2,
        }
    ],
    "Dec - 2019": [
        {
            "id": 183,
            "user_id": 1,
            "type_id": 1,
        }
    ],
    "Oct - 2019": [
        {
            "id": 176,
            "user_id": 1,
            "type_id": 1,
        }
    ]
}

If it were a flat array of objects i would do something like:

typeFilter = data.filter((listing) => {
            if(listing.type_id){
                return listing.type_id == this.state.soldListTypeFilter ;    
            }
        });

UPDATE

I need the output to be in the same format as the input. For example if i were to filter the object by type_id = 2 it would return this:

"soldListings": {
"Jan - 2020": [
    {
        "id": 563,
        "user_id": 1,
        "type_id": 2,
    }
],
"Dec - 2019": [

],
"Oct - 2019": [

]

}

SimonBL
  • 96
  • 8
  • 2
    what is your expected output after sorting ? – Kunal Mukherjee Feb 14 '20 at 17:38
  • 4
    Are you trying to sort or filter the data? What do you want the resulting data to look like? Remember, that objects/dictionaries don't have an order and cannot be "sorted". – Rocket Hazmat Feb 14 '20 at 17:38
  • [This](https://stackoverflow.com/questions/25500316/sort-a-dictionary-by-value-in-javascript) might help. – Rob Moll Feb 14 '20 at 17:39
  • Welcome to Javascript, we called dictionaries "objects" which is sort for JavaScriptObjectNotation objects. – VirxEC Feb 14 '20 at 17:40
  • 4
    @VirxEC JSON Objects are not a thing. JavaScript Objects are objects, JSON is a string representation of an JavaScript Object. – crashmstr Feb 14 '20 at 17:45
  • They use the exact same syntax though (not saying ur wrong) – VirxEC Feb 14 '20 at 17:55
  • @VirxEC No, they don't. You can't define a function in JSON, you can't have expressions as values, you cannot have trailing commas, you cannot have comments, you also cannot have non-string keys. They are not syntactically interchangeable. – mhodges Feb 14 '20 at 19:01

1 Answers1

0

Use keySort function for this problem, for un-integer keys Read https://stackoverflow.com/a/23202095/7146552

keySort( object, sortfn? )

function keySort(obj, sort) {
  var keys = Object.keys(obj);
  keys = keys.sort(sort);
  var sortedObject = {};
  for (var i = 0; i < keys.length; i++) {
    sortedObject[keys[i]] = obj[keys[i]]
  }
  return sortedObject;
}

// Using
var obj = {
  "z:": "Zvalue",
  "a": "AValue"
};
console.log(obj);
obj = keySort(obj);
// or
obj = keySort(obj, (a, b) => {
  return a > b ? 1 : -1
});
console.log(obj)

On your Question Test

function keySort(obj, sort) {
  var keys = Object.keys(obj);
  keys = keys.sort(sort);
  var sortedObject = {};
  for (var i = 0; i < keys.length; i++) {
    sortedObject[keys[i]] = obj[keys[i]]
  }
  return sortedObject;
}
data = {
  "Jan - 2020": [{
      "id": 159,
      "user_id": 1,
      "type_id": 1,
    },
    {
      "id": 173,
      "user_id": 1,
      "type_id": 1,
    },
    {
      "id": 563,
      "user_id": 1,
      "type_id": 2,
    }
  ],
  "Dec - 2019": [{
    "id": 183,
    "user_id": 1,
    "type_id": 1,
  }],
  "Oct - 2019": [{
    "id": 176,
    "user_id": 1,
    "type_id": 1,
  }]
}

data = keySort(data,function(a,b){
  adate = new Date(a);
  bdate = new Date(b);
  return adate>bdate ? 1 : -1;
})
console.log(data);
Hasan Delibaş
  • 390
  • 2
  • 11
  • Isn't there no guarantee that the object will be in the "sorted" order when you traverse over it? Better to just keep the sorted `Object.keys(obj)` and just loop over that when you want to ensure the proper order. – Rocket Hazmat Feb 14 '20 at 19:57
  • Yes, but if keys is un-integer this run rightly. https://stackoverflow.com/a/23202095/7146552 – Hasan Delibaş Feb 14 '20 at 20:23