Changing the order of the Object keys….

If you create a new object from the first object (as the current accepted answer suggests) you will always need to know all of the properties in your object (a maintenance nightmare).

Use Object.assign() instead.

*This works in modern browsers — not in IE or Edge <12.

const addObjectResponse = {
  'DateTimeTaken': '/Date(1301494335000-0400)/',
  'Weight': 100909.090909091,
  'Height': 182.88,
  'SPO2': '222.00000',
  'BloodPressureSystolic': 120,
  'BloodPressureDiastolic': 80,
  'BloodPressurePosition': 'Standing',
  'VitalSite': 'Popliteal',
  'Laterality': 'Right',
  'CuffSize': 'XL',
  'HeartRate': 111,                              // <-----
  'HeartRateRegularity': 'Regular',              // <-----
  'Resprate': 111,    
  'Temperature': 36.6666666666667,
  'TemperatureMethod': 'Oral',    
  'HeadCircumference': '',    
};

// Create an object which will serve as the order template
const objectOrder = {
  'HeartRate': null,
  'HeartRateRegularity': null,
}
  
const addObjectResource = Object.assign(objectOrder, addObjectResource);

The two items you wanted to be ordered are in order, and the remaining properties are below them.

Now your object will look like this:

{           
  'HeartRate': 111,                              // <-----
  'HeartRateRegularity': 'Regular',              // <-----
  'DateTimeTaken': '/Date(1301494335000-0400)/',
  'Weight': 100909.090909091,
  'Height': 182.88,
  'SPO2': '222.00000',
  'BloodPressureSystolic': 120,
  'BloodPressureDiastolic': 80,
  'BloodPressurePosition': 'Standing',
  'VitalSite': 'Popliteal',
  'Laterality': 'Right',
  'CuffSize': 'XL',
  'Resprate': 111,    
  'Temperature': 36.6666666666667,
  'TemperatureMethod': 'Oral',    
  'HeadCircumference': '',    
}

Leave a Comment