3 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
讓我們假設(shè)你有
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貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是一個(gè)簡單易讀的代碼來做到這一點(diǎn):
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貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以使用 justJSON.stringify()和JSON.parse()方法來做到這一點(diǎn):
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);
添加回答
舉報(bào)