| | |
| | | } |
| | | return roleName; |
| | | } |
| | | |
| | | /** |
| | | * 剔除对象中值为 ''、undefined、null 的键,支持嵌套对象 |
| | | * @param {Object} obj - 需要处理的对象 |
| | | * @returns {Object} 处理后的新对象 |
| | | */ |
| | | export function removeEmptyKeys<T extends object>(obj: T) { |
| | | // 如果不是对象或为null,直接返回原值 |
| | | if (obj === null || typeof obj !== 'object') { |
| | | return obj; |
| | | } |
| | | |
| | | // 处理数组(递归处理每个元素) |
| | | if (Array.isArray(obj)) { |
| | | return obj.map((item) => removeEmptyKeys(item)); |
| | | } |
| | | |
| | | // 处理对象 |
| | | const result = {} as T; |
| | | for (const key in obj) { |
| | | if (obj.hasOwnProperty(key)) { |
| | | //@ts-ignore |
| | | const value = removeEmptyKeys(obj[key]); // 递归处理嵌套对象 |
| | | // 只保留非空值(排除''、undefined、null) |
| | | if (value !== '' && value !== undefined && value !== null) { |
| | | result[key] = value; |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |