Changing the order of the Object keys…

后端 未结 11 2045
無奈伤痛
無奈伤痛 2020-11-27 07:21
var addObjectResponse = [{
    \'DateTimeTaken\': \'/Date(1301494335000-0400)/\',
    \'Weight\': 100909.090909091,
    \'Height\': 182.88,
    \'SPO2\': \'222.00000         


        
相关标签:
11条回答
  • 2020-11-27 08:15

    I think that's not possible in JavaScript.

    You can create an array which will contain the field names in your order and you can iterate through this array and fetch the fields from the actual object.

    0 讨论(0)
  • 2020-11-27 08:17

    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': '',    
    }
    
    0 讨论(0)
  • 2020-11-27 08:17

    You can use 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;
    
    0 讨论(0)
  • 2020-11-27 08:18

    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.

    0 讨论(0)
  • 2020-11-27 08:21

    if you want to manually reorder. simply create new object and assign values using old object.

    var newObject= [{
        'DateTimeTaken': addObjectResponse.DateTimeTaken,
        'Weight': addObjectResponse.Weight,
        'Height': addObjectResponse.Height,
        'SPO2': addObjectResponse.SPO2 
    }];
    
    0 讨论(0)
提交回复
热议问题