From ca613e7b34b1cfc1582317acf14c3079b8815914 Mon Sep 17 00:00:00 2001 From: R2m1liA <15258427350@163.com> Date: Fri, 24 Oct 2025 08:01:51 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E6=9D=A1=E7=9B=AE=E7=AD=9B=E9=80=89=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将输出由数组改为单个对象 --- src/helper/collection.test.ts | 14 ++++---------- src/helper/collection.ts | 16 +++++++++------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/helper/collection.test.ts b/src/helper/collection.test.ts index 86ac240..65f4403 100644 --- a/src/helper/collection.test.ts +++ b/src/helper/collection.test.ts @@ -23,15 +23,11 @@ test('filterTranslations - basic functionality', () => { expect(result).toEqual([ { id: 1, - translations: [ - { languages_code: 'en-US', name: 'English Name1' } - ] + translations: { languages_code: 'en-US', name: 'English Name1' } }, { id: 2, - translations: [ - { languages_code: 'en-US', name: 'English Name2' } - ] + translations: { languages_code: 'en-US', name: 'English Name2' } } ]); }); @@ -50,7 +46,7 @@ test('filterTranslations - no matching language', () => { expect(result).toEqual([ { id: 1, - translations: [] + translations: {} } ]); }); @@ -85,9 +81,7 @@ test('filterTranslations - translations in deeper structure', () => { { id: 1, details: { - translations: [ - { languages_code: 'zh-CN', name: '中文名称1' } - ] + translations: { languages_code: 'zh-CN', name: '中文名称1' } } } ]); diff --git a/src/helper/collection.ts b/src/helper/collection.ts index 93c987b..2c73418 100644 --- a/src/helper/collection.ts +++ b/src/helper/collection.ts @@ -33,15 +33,11 @@ type Translation = { * // [ * // { * // id: 1, - * // translations: [ - * // { languages_code: 'zh-CN', name: '中文名称1' } - * // ] + * // translations: { languages_code: 'zh-CN', name: '中文名称1' } * // }, * // { * // id: 2, - * // translations: [ - * // { languages_code: 'zh-CN', name: '中文名称2' } - * // ] + * // translations: { languages_code: 'zh-CN', name: '中文名称2' } * // } * // ] */ @@ -73,7 +69,13 @@ export function filterTranslations(data: T[], langCode: string): T[] { } return false; }); - result[key] = filtered; + + // 展平结果为单个对象或空对象 + if (filtered.length <= 1) { + result[key] = filtered[0] || {}; + } else { + result[key] = filtered; + } } else { // 递归处理其他字段 result[key] = deepFilter(value);