| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | export function removeTextKey(obj) { |
| | | if (typeof obj !== 'object' || obj === null) { |
| | | // 如果是数值字符串,转换为数值 |
| | | if (typeof obj === 'string' && /^-?\d+(\.\d+)?$/.test(obj)) { |
| | | return Number(obj); |
| | | } |
| | | return obj; |
| | | } |
| | | |
| | | if (Array.isArray(obj)) { |
| | | return obj.map(item => removeTextKey(item)); |
| | | } |
| | | |
| | | if (Object.prototype.hasOwnProperty.call(obj, '#text')) { |
| | | // 如果 #text 的值是数值字符串,转换为数值 |
| | | const textValue = obj['#text']; |
| | | return typeof textValue === 'string' && /^-?\d+(\.\d+)?$/.test(textValue) ? Number(textValue) : textValue; |
| | | } |
| | | |
| | | const newObj = {}; |
| | | for (const key in obj) { |
| | | if (Object.prototype.hasOwnProperty.call(obj, key)) { |
| | | newObj[key] = Object.keys(obj[key]).length === 0 ? '' : removeTextKey(obj[key]); |
| | | } |
| | | } |
| | | return newObj; |
| | | } |