[removed] Object Rename Key

前端 未结 24 1406
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  梦毁少年i
    2020-11-22 01:19

    Trying using lodash transform.

    var _ = require('lodash');
    
    obj = {
      "name": "abc",
      "add": "xyz"
    };
    
    var newObject = _.transform(obj, function(result, val, key) {
    
      if (key === "add") {
        result["address"] = val
      } else {
        result[key] = val
      }
    });
    console.log(obj);
    console.log(newObject);
    

提交回复
热议问题