I have an object that I would like to sort based on the votes that it has, and then map over the new / sorted object.
const data = {
"comment-1508872637211" : {
"description" : "Blah",
"votes" : 1
},
"comment-1508872675949" : {
"description" : "Question",
"votes" : 11
},
"comment-1508898578089" : {
"description" : "whatever\n",
"votes" : 5
},
"comment-1508898637092" : {
"description" : "new",
"votes" : 15
},
"comment-1508900306998" : {
"description" : "baj",
"votes" : 0
}
}
console.log(data);
const sortedOnVotes = Object.entries(data);
console.log(sortedOnVotes);
// This is the part I'm getting hung up on
const newDataObject = sortedOnVotes.map(([key, value]) => value).sort();
console.log(newDataObject)
Ultimately I would love for this new object to still retain the comment-### key and be filtered based on the amount of votes that it has. For example the newDataObject should return something like this:
const newDataObject = {
"comment-1508900306998" : {
"description" : "baj",
"votes" : 0
},
"comment-1508872637211" : {
"description" : "Blah",
"votes" : 1
},
"comment-1508898578089" : {
"description" : "whatever\n",
"votes" : 5
},
"comment-1508872675949" : {
"description" : "Question",
"votes" : 11
}
"comment-1508898637092" : {
"description" : "new",
"votes" : 15
}
}
I think I'm on the right track by using Object.values or Object.entries but I'm really getting hung up on it.
Any help would really be appreciated, thanks!
lodash/sort, easy to use and simple to configure. See lodash here.