Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the order of the Object keys....

Tags:

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...

like image 915
John Cooper Avatar asked Aug 05 '11 16:08

John Cooper


2 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.

 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': '',    
}
like image 134
Jeff Vdovjak Avatar answered Oct 21 '22 07:10

Jeff Vdovjak


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}
like image 41
Ben K. Avatar answered Oct 21 '22 08:10

Ben K.