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": [

]

}

7
  • 2
    what is your expected output after sorting ? Commented Feb 14, 2020 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". Commented Feb 14, 2020 at 17:38
  • This might help. Commented Feb 14, 2020 at 17:39
  • Welcome to Javascript, we called dictionaries "objects" which is sort for JavaScriptObjectNotation objects. Commented Feb 14, 2020 at 17:40
  • 4
    @VirxEC JSON Objects are not a thing. JavaScript Objects are objects, JSON is a string representation of an JavaScript Object. Commented Feb 14, 2020 at 17:45

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Yes, but if keys is un-integer this run rightly. stackoverflow.com/a/23202095/7146552

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.