[removed] Object Rename Key

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

    While this does not exactly give a better solution to renaming a key, it provides a quick and easy ES6 way to rename all keys in an object while not mutating the data they contain.

    let b = {a: ["1"], b:["2"]};
    Object.keys(b).map(id => {
      b[`root_${id}`] = [...b[id]];
      delete b[id];
    });
    console.log(b);
    

提交回复
热议问题