0

How I can sort my object after add one value

{
2017-12-18:{210: {…}},
2017-12-20:{211: {…}},
2017-12-21:{186: {…}, 188: {…}, 189: {…}, 190: {…}, 201: {…}}
}

And now I add object 2017-12-19:{220: {…}}, but how I can add this after date 2017-12-18 ?

dhilt
  • 18,707
  • 8
  • 70
  • 85
Paweł Baca
  • 814
  • 2
  • 15
  • 28

2 Answers2

0

Take a look at this answer for sort object by property name

You can just run that function after adding a new "line" to your object.

I would advise you to make sure you are not overwriting and existing property in your object before you attempt to add a new one.

Eyal Alsheich
  • 452
  • 4
  • 12
0

This work

let sortedData = function sortObjectKeys(obj){
    return Object.keys(obj).sort().reduce((acc,key)=>{
        acc[key]=obj[key];
        return acc;
    },{});
}
Paweł Baca
  • 814
  • 2
  • 15
  • 28