mirror of
https://git.ddmt.top/ddmt/ddmt-tool.git
synced 2026-03-23 05:17:06 +00:00
feat(Tool): 添加重命名对象属性的功能
- 新增 renameObjectKeys 函数,用于根据字段映射表重新命名对象的属性 - 函数接受过滤后的行数据数组和字段映射表作为参数 - 返回一个新的数组,其中对象的属性根据字段映射表进行了重命名 - 如果字段不在映射表中,则直接跳过,不加入新对象
This commit is contained in:
@@ -186,4 +186,25 @@ export function objectToJSON(ObjectText) {
|
||||
} catch (error) {
|
||||
console.error("解析失败:", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字段映射表重新命名对象的属性
|
||||
* renameObjectKeys
|
||||
* @param {Array} filteredRows - 过滤后的行数据数组,每个元素是一个对象
|
||||
* @param {Object} fieldMapping - 字段映射表,键是原始字段名,值是新的字段名
|
||||
* @returns {Array} - 返回一个新的数组,其中对象的属性根据字段映射表进行了重命名
|
||||
*/
|
||||
export function renameObjectKeys(filteredRows, fieldMapping) {
|
||||
return filteredRows.map(row => {
|
||||
return Object.keys(row).reduce((acc, key) => {
|
||||
// 如果字段在映射表中,则使用映射的字段名
|
||||
if (fieldMapping[key]) {
|
||||
const newKey = fieldMapping[key];
|
||||
acc[newKey] = row[key];
|
||||
}
|
||||
// 如果字段不在映射表中,则直接跳过(不加入新对象)
|
||||
return acc;
|
||||
}, {});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user