[removed] Object Rename Key

前端 未结 24 1384
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 00:18

Is there a clever (i.e. optimized) way to rename a key in a javascript object?

A non-optimized way would be:

o[ new_key ] = o[ old_key ];
delete o[ o         


        
相关标签:
24条回答
  • 2020-11-22 01:03

    In case someone needs to rename a list of properties:

    function renameKeys(obj, newKeys) {
      const keyValues = Object.keys(obj).map(key => {
        const newKey = newKeys[key] || key;
        return { [newKey]: obj[key] };
      });
      return Object.assign({}, ...keyValues);
    }
    

    Usage:

    const obj = { a: "1", b: "2" };
    const newKeys = { a: "A", c: "C" };
    const renamedObj = renameKeys(obj, newKeys);
    console.log(renamedObj);
    // {A:"1", b:"2"}
    
    0 讨论(0)
  • 2020-11-22 01:03
    const clone = (obj) => Object.assign({}, obj);
    
    const renameKey = (object, key, newKey) => {
    
        const clonedObj = clone(object);
      
        const targetKey = clonedObj[key];
      
      
      
        delete clonedObj[key];
      
        clonedObj[newKey] = targetKey;
      
        return clonedObj;
         };
    
      let contact = {radiant: 11, dire: 22};
    
    
    
    
    
    contact = renameKey(contact, 'radiant', 'aplha');
    
    contact = renameKey(contact, 'dire', 'omega');
    
    
    
    console.log(contact); // { aplha: 11, omega: 22 };
    
    0 讨论(0)
  • 2020-11-22 01:05

    The most complete (and correct) way of doing this would be, I believe:

    if (old_key !== new_key) {
        Object.defineProperty(o, new_key,
            Object.getOwnPropertyDescriptor(o, old_key));
        delete o[old_key];
    }
    

    This method ensures that the renamed property behaves identically to the original one.

    Also, it seems to me that the possibility to wrap this into a function/method and put it into Object.prototype is irrelevant regarding your question.

    0 讨论(0)
  • 2020-11-22 01:05

    If you don’t want to mutate your data, consider this function...

    renameProp = (oldProp, newProp, {[oldProp]:old, ...others}) => ({
        [newProp]: old,
        ...others
    })
    

    A thorough explanation by Yazeed Bzadough https://medium.com/front-end-hacking/immutably-rename-object-keys-in-javascript-5f6353c7b6dd


    0 讨论(0)
  • 2020-11-22 01:05

    I'd do something like this:

    function renameKeys(dict, keyMap) {
      return _.reduce(dict, function(newDict, val, oldKey) {
        var newKey = keyMap[oldKey] || oldKey
        newDict[newKey] = val 
        return newDict
      }, {})
    }
    
    0 讨论(0)
  • 2020-11-22 01:08

    Some of the solutions listed on this page have some side-effects:

    1. affect the position of the key in the object, adding it to the bottom (if this matters to you)
    2. would not work in IE9+ (again, if this matters to you)

    Here is a solution which keeps the position of the key in the same place and is compatible in IE9+, but has to create a new object and may not be the fastest solution:

    function renameObjectKey(oldObj, oldName, newName) {
        const newObj = {};
    
        Object.keys(oldObj).forEach(key => {
            const value = oldObj[key];
    
            if (key === oldName) {
                newObj[newName] = value;
            } else {
                newObj[key] = value;
            }
        });
    
        return newObj;
    }
    

    Please note: IE9 may not support forEach in strict mode

    0 讨论(0)
提交回复
热议问题