/**
* @param {string} 数组或者对象大写横线转为驼峰式
* @returns {Boolean}
*/
export function convertToCamelCase(data){
const convert = (str) => {
return str.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase()
.replace('-', '')
.replace('_', '');
});
};
if (Array.isArray(data)) {
return data.map((item) => {
if (typeof item === 'object' && item !== null) {
const newItem = {};
for (const key in item) {
if (item.hasOwnProperty(key)) {
const newKey = convert(key.toLowerCase()); // 将首字母转换为小写
newItem[newKey] = convertToCamelCase(item[key]);
}
}
return newItem;
}
return item;
});
} else if (typeof data === 'object' && data !== null) {
const newData = {};
for (const key in data) {
if (data.hasOwnProperty(key)) {
const newKey = convert(key.toLowerCase()); // 将首字母转换为小写
newData[newKey] = convertToCamelCase(data[key]);
}
}
return newData;
}
return data;
};
数组或者对象大写横线转为驼峰式
最新推荐文章于 2024-09-11 22:31:16 发布