How keep the order of items in an object

It gets an object with quite specific keys from the backend. I have to change some values ​​but only to the property where I should break. However, I must keep the order.
The code below works, but I have a problem in my project – different browser? Don’t know. "For of" starts from key "14D". How can I be sure, how can I keep order? Because of specific keys, I cannot sort it.

let updatedData = {};
const dataFromBd = {
  '1M': {
    name: 'anna'
  },
  '1Y': {},
  '2Y': {},
  '3M': {},
  '3Y': {},
  '4Y': {},
  '5Y': {},
  '6M': {},
  '7Y': {},
  '10Y': {},
  '14D': {},
  '15Y': {},
  '>20Y': {}
};

for (let [key, value] of Object.entries(dataFromBd)) {
  updatedData[key] = 'hello';
  if (key === '10Y') break;
}

console.log('data', updatedData);

https://codepen.io/Whity/pen/KKNvKQq

51 thoughts on “How keep the order of items in an object”

  1. 651091 210095Spot on with this write-up, I really suppose this site needs much more consideration. probably be once much more to learn way much more, thanks for that info. 881091

    Reply

Leave a Comment