1

I'm using redux and a piece of my state has this structure:

{cityId: {name:cityName, population: numberOfPeople}}

something like:

{
1324:{name:"Clagary", population:1234, id: 1324},
46283: {name:"Edmonton", population: 5678, id: 46283}
}

Everything was fine till i needed to show them in a list with the same order that they have on the main object. At this point, I figured out that objects will auto-sort the keys if they are numbers. I tried to change them to string by putting them inside a "" but it didn't work ({"1324":{...}}). So i tried adding and underline character as:

({"_1324":{...}})

Now it's working but I just have to double-check with some of you experts here to make sure that this is a good practice and it's normal to do it this way or if there is any better way to deal with numbers as keys in an object when the order matters.

So I'm asking react/redux experts to see if they would move to other solutions when they need the order or would just do what i did.

P.S. I really don't want to go back to use an array as this type of object store is much easier and handy for us in many ways.

  • _"I tried to change them to string by putting them inside a `""`"_, just an fyi all object keys get internally converted to strings (unless it is a `Symbol`) – Patrick Evans Jan 07 '20 at 19:10
  • no that's just about using _ as to start a name, I am asking if this a good way to prevent object key auto-sorting. – ILoveReactAndNode Jan 07 '20 at 19:11
  • "i needed to show them in a list with the same order that they have on the main object" - there is your problem. In that case, an object is not the data-structure of choice. You can e.g. use a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map), or some even more suitable structure. – ASDFGerte Jan 07 '20 at 19:11
  • @ASDFGerte thanks i know that but in Redux it's suggested to use objects the way that i did to store data, so I'm asking react-redux experts to see if they move to other solutions when they need the order or just di what i did – ILoveReactAndNode Jan 07 '20 at 19:14
  • wouldn't it be related to this question? => https://stackoverflow.com/questions/59581719/javascript-appending-object-doesnt-guarantee-order – Mister Jojo Jan 07 '20 at 19:16
  • @MisterJojo thanks i saw that question but in Redux it's suggested to use objects the way that i did to store data, so I'm asking react-redux experts to see if they move to other solutions when they need the order or just di what i did – ILoveReactAndNode Jan 07 '20 at 19:18
  • @jmargolisvt thanks but this link was already discussed above in the comments. – ILoveReactAndNode Jan 07 '20 at 19:20
  • That's an auto-gen comment when you mark a question as a dupe. I don't think there's anything special to react or redux in your question. I think that link tells you what you are looking for. – jmargolisvt Jan 07 '20 at 19:25
  • In javascript, using object property order to guarantee some order of iteration over a literal in some later method, is generally bad practice. I'd be confused, if that would be the suggested way to go in redux (yet i don't have much knowledge about redux). I'd much prefer using e.g. some indexed structure, that then gives e.g. an array to redux, to preserve the order. With a `Map` (as mentioned above), you can access single keys in avg O(1), similar to an object, yet easily iterate it in insertion order, or get an array from it, e.g. with the spread operator. – ASDFGerte Jan 07 '20 at 19:25
  • whether react-redux or anything else, this problem will always remain linked to memory management and the order of the keys of json objects will always be distributed according to the good will of the free embedding granted by the system – Mister Jojo Jan 07 '20 at 19:35
  • @MisterJojo object property order is set in stone for almost all applications since ES6, and for all since ES2020 (except some very harsh edge cases). – ASDFGerte Jan 07 '20 at 19:39

0 Answers0