[removed] Object Rename Key

前端 未结 24 1369
爱一瞬间的悲伤
爱一瞬间的悲伤 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 00:56

    You can try lodash _.mapKeys.

    var user = {
      name: "Andrew",
      id: 25,
      reported: false
    };
    
    var renamed = _.mapKeys(user, function(value, key) {
      return key + "_" + user.id;
    });
    
    console.log(renamed);

提交回复
热议问题