开发中后端返回下划线数据,要不要统一转驼峰?

时间:2025-03-26 08:35:15
const toCamelCase = (obj) => { if (Array.isArray(obj)) { return obj.map(toCamelCase); } else if (obj !== null && typeof obj === 'object') { return Object.keys(obj).reduce((acc, key) => { const camelKey = key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()); acc[camelKey] = toCamelCase(obj[key]); return acc; }, {}); } return obj; }; // 示例: const dbData = { user_name: "张三", order_list: [] }; console.log(toCamelCase(dbData)); // { userName: "张三", orderList: [] }