From 5d696abd78572220762544e4b907b8e903cecd80 Mon Sep 17 00:00:00 2001 From: ddmt Date: Mon, 24 Feb 2025 22:13:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(Tool):=20=E6=B7=BB=E5=8A=A0=E9=87=8D?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E5=AF=B9=E8=B1=A1=E5=B1=9E=E6=80=A7=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 renameObjectKeys 函数,用于根据字段映射表重新命名对象的属性 - 函数接受过滤后的行数据数组和字段映射表作为参数 - 返回一个新的数组,其中对象的属性根据字段映射表进行了重命名 - 如果字段不在映射表中,则直接跳过,不加入新对象 --- Tool/number.js | 21 +++++++++++++++++++++ index.js | 9 ++++++--- types/index.d.ts | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Tool/number.js b/Tool/number.js index 0b3b18a..bf2106b 100644 --- a/Tool/number.js +++ b/Tool/number.js @@ -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; + }, {}); + }); } \ No newline at end of file diff --git a/index.js b/index.js index 36218f7..48eda98 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,8 @@ import { parseNginxLog, toCookiesArray, updateCookies, - objectToJSON + objectToJSON, + renameObjectKeys } from './Tool/number.js'; // ES Module 导出 @@ -38,7 +39,8 @@ export { parseNginxLog, toCookiesArray, updateCookies, - objectToJSON + objectToJSON, + renameObjectKeys }; // CommonJS 导出 @@ -54,7 +56,8 @@ if (typeof module !== 'undefined' && module.exports) { parseNginxLog, toCookiesArray, updateCookies, - objectToJSON + objectToJSON, + renameObjectKeys }; } diff --git a/types/index.d.ts b/types/index.d.ts index a8d44df..5a9221a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -14,4 +14,5 @@ declare module 'ddmt-tool' { export function toCookiesArray(cookies: string): { [key: string]: string }; export function updateCookies(cookies: string, setCookieArray: string[]): string; export function objectToJSON(ObjectText: string): string | undefined; + export function renameObjectKeys(filteredRows: Array<{ [key: string]: any }>, fieldMapping: { [key: string]: string }): Array<{ [key: string]: any }>; } \ No newline at end of file