对象key的驼峰转下划线
Sunny-117 opened this issue · comments
实现函数:将对象小驼峰式的属性名转下划线如:userName -> user_name
function transform(str) {
let res = "";
for (let i = 0; i < str.length; i++) {
if (str[i] >= "A" && str[i] <= "Z") {
res += "_" + str[i].toLowerCase();
} else {
res += str[i];
}
}
return res;
}
console.log(transform("strAbc"));
str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
let userName = "userName";
userName = userName.replace(/([a-z])([A-Z])/g, function(text, $1, $2){
return $1 + "_" + $2.toLowerCase();
})
console.log(userName)