//形式一:普通方法导出
export const transHump = list => {
let resultList = [];//最终输出的数组
list.map(o => {
let tmpObj = {};//本轮最终被转换好的对象
for (let i in o) {
let key = '';
let tmp = i.split('_');//把键按下划线分割成数组
let lowcaseList = [];//转换好的键,如:REG_NAME 结果为:['reg','Name']
tmp.map(m =>
lowcaseList.push(m.toLowerCase()));//遍历键,并转换成小写
//如果键由多余一个单词构成
if (lowcaseList.length > 1) {
for (let i = 1, len = lowcaseList.length; i < len; i++) {
lowcaseList[i] = lowcaseList[i].slice(0, 1).toUpperCase() + lowcaseList[i].slice(1);//从第二个单词开始,每个单词的首字母转换成大写
}
}
key = lowcaseList.join('');//拼接
tmpObj[key] = o[i];//把键对应的值赋值到新对象中
}
resultList.push(tmpObj);//把本轮转换好的对象push到最终要输出的数组中
});
// console.log(resultList);
return resultList;
}
//形式二:类导出
//大写带下划线转驼峰,如:REG_NAME 结果为:regName
class Hump {
constructor(list) {
this.list = list;
}
transHump() {
let resultList = [];
this.list.map(o => {
let tmpObj = {};
for (let i in o) {
let key = this.toHump(i);
tmpObj[key] = o[i];
}
resultList.push(tmpObj);
});
// console.log(resultList);
return resultList;
}
toHump(i) {
/**
* 功能:大写带下划线转驼峰,如:REG_NAME 结果为:regName
*/
let tmp = i.split('_');
let lowcaseList = [];
tmp.map(m => lowcaseList.push(m.toLowerCase()));
if (lowcaseList.length > 1) lowcaseList.map(n => n = n.slice(0, 1).toUpperCase() + n.slice(1));
return lowcaseList.join('');
}
}
export const transHump = list => {
return new Hump(list).transHump();
}