JavaScript/TypeScript中JSON响应键转换的高效优化方案问询
寻求更轻量高效的JSON键转换实现方案
我已经实现了JSON响应的键转换功能,但想找到更轻量、高效的实现方式,欢迎提供更好的思路与建议。
键值映射表
{ "score": "RelevantScore", "headline": "Headline", "subtitle1": "PublishedDate", "subtitle2": "Stage", "body": "Summary", "labels": "Tag" }
转换目标
将输入JSON中的旧属性(映射表中的VALUE值)替换为对应的新KEY,不匹配的键直接保留。
接收的输入JSON响应
{ "Data": [{ "RowId": 1, "Headline": "some data", "PublishedDate": "2022-09-29", "Summary": " some data", "SourceURL": "https://www.google.com", "RelevantScore": 0, "Stage": null, "Tag": "" }] }
转换后预期输出
{ "Data": [{ "RowId": 1, "headline": "some random headline", "subtitle1": "2022-09-29", "body": "some data", "SourceURL": "https://www.google.com", "score": 0, "subtitle2": null, "labels": "" }] }
现有实现代码
单个键替换函数
function renameKey(obj, oldKey, newKey) { obj[newKey] = obj[oldKey]; delete obj[oldKey]; }
调用示例
const json = ` [ { "RelevantScore":"5078c3a803ff4197dc81fbfb" } ] `; const arr = JSON.parse(json); arr.forEach(obj => renameKey(obj, 'RelevantScore', 'Score')); const updatedJson = JSON.stringify(arr);
执行结果
[ { "Score":"5078c3a803ff4197dc81fbfb" } ]
优化实现方案
方案1:批量原地转换(内存友好)
先把映射表反转,方便通过旧键快速查找新键,然后批量处理所有需要转换的键,避免多次调用单个替换函数:
// 定义映射表 const keyMap = { "score": "RelevantScore", "headline": "Headline", "subtitle1": "PublishedDate", "subtitle2": "Stage", "body": "Summary", "labels": "Tag" }; // 反转映射表:旧键 -> 新键 const reverseKeyMap = Object.fromEntries( Object.entries(keyMap).map(([newKey, oldKey]) => [oldKey, newKey]) ); // 批量转换函数 function transformKeys(obj) { for (const [oldKey, newKey] of Object.entries(reverseKeyMap)) { if (oldKey in obj) { obj[newKey] = obj[oldKey]; delete obj[oldKey]; } } return obj; } // 使用方式 const inputJson = `{ "Data": [{ "RowId": 1, "Headline": "some data", "PublishedDate": "2022-09-29", "Summary": " some data", "SourceURL": "https://www.google.com", "RelevantScore": 0, "Stage": null, "Tag": "" }] }`; const data = JSON.parse(inputJson); data.Data = data.Data.map(transformKeys); const outputJson = JSON.stringify(data, null, 2);
方案2:纯函数生成新对象(无副作用)
如果不想修改原对象,使用reduce生成新对象,更符合函数式编程风格,避免副作用:
// 同样的映射表和反转映射表 const keyMap = { "score": "RelevantScore", "headline": "Headline", "subtitle1": "PublishedDate", "subtitle2": "Stage", "body": "Summary", "labels": "Tag" }; const reverseKeyMap = Object.fromEntries( Object.entries(keyMap).map(([newKey, oldKey]) => [oldKey, newKey]) ); // 纯转换函数 function transformKeysPure(obj) { return Object.entries(obj).reduce((newObj, [key, value]) => { // 有对应新键就用新键,否则保留原键 newObj[reverseKeyMap[key] || key] = value; return newObj; }, {}); } // 使用方式 const inputJson = `{ "Data": [{ "RowId": 1, "Headline": "some data", "PublishedDate": "2022-09-29", "Summary": " some data", "SourceURL": "https://www.google.com", "RelevantScore": 0, "Stage": null, "Tag": "" }] }`; const data = JSON.parse(inputJson); data.Data = data.Data.map(transformKeysPure); const outputJson = JSON.stringify(data, null, 2);
方案对比
- 方案1:原地修改对象,内存占用更低,适合处理大量数据场景;但会修改原对象,可能引发副作用。
- 方案2:生成新对象,不影响原数据,代码更简洁安全;需要额外内存存储新对象,数据量极大时需考虑内存开销。
内容的提问来源于stack exchange,提问作者user20699854
相关产品推荐
相关产品推荐

