Reordering the keys of the Object ....

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 that I get from the back, now I want to reorder the object. I don't want to sort by name or size ... I just want to manually reorder ...

+6
source share
9 answers

if you want to manually reorder. just create a new object and assign values ​​using the old object.

var newObject= [{
    'DateTimeTaken': addObjectResponse.DateTimeTaken,
    'Weight': addObjectResponse.Weight,
    'Height': addObjectResponse.Height,
    'SPO2': addObjectResponse.SPO2 
}];
0
source

/ JavaScript . , . JS , . (for x in var)

+11

, , jQuery .insertAfter(). :

//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;
}

:

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}
+6

JavaScript . , ECMAScript , , , . , Chrome , : . , , , , , .

, ( ).

+5

Object.keys():

var obj = {
    a: 1,
    b: 2,
    c: 4
};

var new_obj = {};

Object.keys(obj)
.sort(function(a, b) {
    /** Insert your custom sorting function here */
    return a - b;
})
.forEach(function(key) {
    new_obj[key] = obj[key];
});

obj = new_obj;
+4

, JavaScript.

You can create an array that will contain the field names in your order, and you can iterate over this array and get the fields from the actual object.

+1
source

Just access the object keys in the order you want:

aKeys = [
  addObjectResponse[0].DateTimeTaken,
  addObjectResponse[0].Weight,
  addObjectResponse[0].Height,
  ...etc...
]
0
source
function orderKeys(obj, keys){
    const newObj = {};
    for(let key of keys){
        newObj[key] = obj[key];
    }
    return newObj;
}
0
source

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 the code: https://codepen.io/zhangyanwei/pen/QJeRxB

0
source

All Articles