Changing the order of the Object keys....

Viewed 59053
var 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': '',    
}];

This is a sample object which i am getting from back end, now i want to change the order of the object. I don't want to sort by name or size... i just want to manually change the order...

11 Answers

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': '',    
}

I like the approved answer by Chamika Sandamal. Here's a simple function that uses their same logic with a little be of freedom to change the order as you need it.

function preferredOrder(obj, order) {
    var newObject = {};
    for(var i = 0; i < order.length; i++) {
        if(obj.hasOwnProperty(order[i])) {
            newObject[order[i]] = obj[order[i]];
        }
    }
    return newObject;
}

You give it an object, and an array of the key names you want, and returns a new object of those properties arranged in that order.

var data = {
    c: 50,
    a: 25,
    d: 10,
    b: 30
};

data = preferredOrder(data, [
    "a",
    "b",
    "c",
    "d"
]);

console.log(data);

/*
    data = {
        a: 25,
        b: 30,
        c: 50,
        d: 10
    }
*/

I'm copying and pasting from a big JSON object into a CMS and a little bit of re-organizing of the source JSON into the same order as the fields in the CMS has saved my sanity.

If you do not want to create a new object, you can use the following code snippet.

function orderKey(obj, keyOrder) {
    keyOrder.forEach((k) => {
        const v = obj[k]
        delete obj[k]
        obj[k] = v
    })
}

here is a codepen: https://codepen.io/zhangyanwei/pen/QJeRxB

function orderKeys(obj, keys){
    const newObj = {};
    for(let key of keys){
        newObj[key] = obj[key];
    }
    return newObj;
}
Related