1

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.

  • 2
    The order of object properties isn't guaranteed to be used when printing the object. Use an array if order is important. – Barmar Jun 11 '19 at 21:30
  • Use any array instead. `Object.values(o).sort((x, y) => y - x) ` – Aluan Haddad Jun 11 '19 at 21:31
  • 1
    There's no such thing as a _JSON Object_. JSON is always a String. You have a plain Object. – Mulan Jun 11 '19 at 21:36
  • Or use a `Map`. – Barmar Jun 11 '19 at 21:37
  • @Barmar, I forgot to say that i need those keys. I will try to implement the code using `map()`, thanks for the help man. – Marco Antonio Occhialini Filho Jun 11 '19 at 21:45
  • I never said to get rid of the keys. You can make an array of objects: `[{key: 1079, value: "2019-05-16 14:00 - Centro Universitário Maria Antônia"}, {key: 1078, value: "2019-05-16 11:00 - Centro Universitário Maria Antônia"}, ...]` – Barmar Jun 11 '19 at 21:47
  • Note also that keys in JavaScript objects are always strings or Symbols. So your first entry is actually `"70": "2014-02-20 10:00 - Centro Universitário Maria Antônia"`. This is important because there are situations where numbers-as-strings sort differently from actual numbers. – Heretic Monkey Jun 11 '19 at 22:58

3 Answers3

1

I hope this helps. I'm not even sure about the performance. Feel free to vote down as long you give me feedback.

Let's say you have the json var:

const reversedKeys = Object.keys(json).reverse();
const reversedJson = reversedKeys.map(e => ({[e]: json[Number(e)]}) );
Luillyfe
  • 6,183
  • 8
  • 36
  • 46
0

Consider that the order of the elements in an object is NOT preserved. In practice, JavaScript object is a dictionary, where, internally, data are not stored in a sequential manner such as in an Array, and values are accessed using a hash function that takes a key as input and - in a simplified sense, gives the "position" of the value inside the structure.

Dictionary is a Data Structure that does NOT preserve ordering (yet, it provides fast data retrieval via indexing), not in JavaScript, or in any other programming language.

I suggest that you use an array for preserving order.

Maybe an array containing different objects could be a solution for you. You can then sort the array based on the key of the object. Here is a "dummy" example:

var arr = [{1: "Test1"}, {2: "Test2"}]
arr.sort((obj1, obj2) => {
  return Object.keys(obj2)[0] - Object.keys(obj1)[0];
});
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
0

As adam-lassek said in this post Javascript associative arrays are unordered. You must write your own function to reverse your array as did yossi-neiman in this post

Meziane
  • 1,586
  • 1
  • 12
  • 22