// 大写转驼峰
convertUnderscoreToCamelCase(str) {
return str.toLowerCase().replace(/_(.)/g, function (match, group1) {
return group1.toUpperCase();
});
},
示例
ACC_FACT 转换成 accFact
第一种方法
// 循环数组
for (let i = 0; i < arrList.length; i++) {
let obj = {};
Object.keys(arrList[i]).forEach((item) => {
// 大写转小写
const convertedStr = item.toLowerCase().replace(/_/g, '');
// 小写转驼峰
const toCamelCase = convertedStr.replace(/[-_\s]+(.)?/g, (match, group1) =>
group1 ? group1.toUpperCase() : '');
obj[toCamelCase] = arrList[i][item];
});
params.push(obj);
}
第二种方法
convertPropsToCamelCase(arr) {
return arr.map((obj) => {
const newObj = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
// 将下划线后边的第一个字母变成大写
const snakeCaseKey = key.toLowerCase();
const words = snakeCaseKey.split("_");
const camelCaseKey = words.reduce((result, word, index) => {
if (index === 0) {
return word;
} else {
return result + word.charAt(0).toUpperCase() + word.slice(1);
}
}, "");
newObj[camelCaseKey] = obj[key];
}
}
return newObj;
});
},