I am new to JavaScript programming, and I am having a headache trying to figure it out how to reverse keys of a single JSON object. This is the object that I'm trying to reverse:
{70: "a", 276: "b ", 277: "c ", 688: "d", 841: "e", 842: "f", 843: "g", 1078: "h", 1079: "i"} I am running this code on localhost. I am analysing the console.logs with google chrome developer tools( F12 ). I can also use Jquery in this script.
This is what I've tried so far:
//The object is stored in a variable called json var json = {70: "a", 276: "b ", 277: "c ", 688: "d", 841: "e", 842: "f", 843: "g", 1078: "h", 1079: "i"}; var entries = Object.entries(json); entries.reverse(); var newjson = Object.fromEntries(entries); console.log(newjson); When I run this code, the output is the same as the old json variable, is inaltered.
And this is the expected console.log()
{ 1079: "i", 1078: "h", 843: "g", 842: "f", 841: "e", 688: "d", 277: "c", 276: "b", 70: "a", } Much thanks to anyone who tries to help me.
Object.values(o).sort((x, y) => y - x)Map.map(), thanks for the help man.