3 回答
TA贡献1877条经验 获得超1个赞
让我们假设你有
var jsonData=
{
key1:'val1',
key2:'val2',
C_100:1,
C_101:2,
C_102:3,
}
然后你需要按如下方式处理
Object.entries(jsonData).map(e => [e[0].replace(/^C_/,''), e[1]]).reduce((p,n) => ({ ...p, [n[0]]: n[1] }), {})
要得到
{100: 1, 101: 2, 102: 3, key1: "val1", key2: "val2"}
TA贡献1876条经验 获得超6个赞
这是一个简单易读的代码来做到这一点:
var jsonData=
{
key1:"val1",
key2:"val2",
C_100:1,
C_101:2,
C_102:3,
}
var modifiedData = {};
for (key of Object.keys(jsonData)) {
var newKey = key.replace("C_", "");
modifiedData[newKey] = jsonData[key];
}
console.log(modifiedData) // prints var jsonData=
{
key1:"val1",
key2:"val2",
C_100:1,
C_101:2,
C_102:3,
}
var modifiedData = {};
for (key of Object.keys(jsonData)) {
var newKey = key.replace("C_", "");
modifiedData[newKey] = jsonData[key];
}
console.log(modifiedData) // prints { '100': 1, '101': 2, '102': 3, key1: 'val1', key2: 'val2' }
TA贡献1998条经验 获得超6个赞
您可以使用 justJSON.stringify()和JSON.parse()方法来做到这一点:
let jsonData =
{
key1: val1,
key2: val2,
C_100: 1,
C_101: 2,
C_102: 3,
};
let string = JSON.stringify(jsonData);
string = string.replace(/C_/g, "");
let obj = JSON.parse(string);
添加回答
举报