Changing the order of the Object keys....

I wrote this small algorithm which allows to move keys, it's like jQuery .insertAfter() method. You have to provide:

//currentKey: the key you want to move
//afterKey: position to move-after the currentKey, null or '' if it must be in position [0]
//obj: object


function moveObjectElement(currentKey, afterKey, obj) {
    var result = {};
    var val = obj[currentKey];
    delete obj[currentKey];
    var next = -1;
    var i = 0;
    if(typeof afterKey == 'undefined' || afterKey == null) afterKey = '';
    $.each(obj, function(k, v) {
        if((afterKey == '' && i == 0) || next == 1) {
            result[currentKey] = val; 
            next = 0;
        }
        if(k == afterKey) { next = 1; }
        result[k] = v;
        ++i;
    });
    if(next == 1) {
        result[currentKey] = val; 
    }
    if(next !== -1) return result; else return obj;
}

Example:

var el = {a: 1, b: 3, c:8, d:2 }
el = moveObjectElement('d', '', el); // {d,a,b,c}
el = moveObjectElement('b', 'd', el); // {d,b,a,c}

You can't order JavaScript object key/value pairs. It's stored in its own internal format, so you should never rely on the order of that. In JS, everything is an Object, even an Array. So sometimes you can introduce bugs when using array notation and object notation together (for x in var)


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.

 let 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
    let objectOrder = {
        'HeartRate': null,
        'HeartRateRegularity': null,
    }

    addObjectResource = Object.assign(objectOrder, addObjectResource);

Now the two items you wanted 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': '',    
}

Tags:

Javascript