Changing the order of the Object keys…

后端 未结 11 2051
無奈伤痛
無奈伤痛 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:01
    function orderKeys(obj, keys){
        const newObj = {};
        for(let key of keys){
            newObj[key] = obj[key];
        }
        return newObj;
    }
    
    0 讨论(0)
  • 2020-11-27 08:04

    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

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

    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)

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

    Just refer to the object keys in the order that you like:

    aKeys = [
      addObjectResponse[0].DateTimeTaken,
      addObjectResponse[0].Weight,
      addObjectResponse[0].Height,
      ...etc...
    ]
    
    0 讨论(0)
  • 2020-11-27 08:14

    2020 Update

    The answer below was correct at time of writing in 2011. However, since ES6, enumeration order has been specified as part of the language. Here's a nice article summarising this: https://2ality.com/2015/10/property-traversal-order-es6.html

    Original answer

    Properties of an object in JavaScript do not have an order. There may appear to be an order in some browsers but the ECMAScript specification defines object property enumeration order as being implementation-specific so you should not assume one browser's behaviour will be the same as another's. Chrome, for example, does not use the same ordering as some other browsers: see this lengthy bug report for at least as much discussion of this issue as you could possibly want.

    If you need a specific order, use an array, or two arrays (one for keys and one for values).

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

    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}
    
    0 讨论(0)
提交回复
热议问题