What's the quickest way to rename all property names in a Javascript object?

后端 未结 3 407
不知归路
不知归路 2021-01-29 11:44

Say, I have an object that looks like this:

const a = {
  prop1: \"val1\",
  prop2: \"val2\",
  prop3: \"val3\",
  prop4: \"val4\",
}

And I wan

3条回答
  •  心在旅途
    2021-01-29 12:22

    You can do:

    const a = {
      prop1: "val1",
      prop2: "val2",
      prop3: "val3",
      prop4: "val4",
    };
    const b = {};
    Object.keys(a).forEach(k => b[`something_${k}`] = a[k]);
    
    console.log(b);

提交回复
热议问题