function camelCaseKeysToUnderscore(obj){
if (typeof(obj) != "object") return obj;
for(var oldName in obj){
// Camel to underscore
newName = oldName.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
// Only process if names are different
if (newName != oldName) {
// Check for the old property name to avoid a ReferenceError in strict mode.
if (obj.hasOwnProperty(oldName)) {
obj[newName] = obj[oldName];
delete obj[oldName];
}
}
// Recursion
if (typeof(obj[newName]) == "object") {
obj[newName] = camelCaseKeysToUnderscore(obj[newName]);
}
}
return obj;
}
import { snakeCase } from 'lodash'; // or use the regex in the accepted answer
camelCase = obj => {
const camelCaseObj = {};
for (const key of Object.keys(obj)){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
camelCaseObj[snakeCase(key)] = obj[key];
}
}
return camelCaseObj;
};
export function camelCaseKeysToSnake(obj){
if (typeof(obj) != "object") return obj;
let newObj = {...obj}
for(var oldName in newObj){
// Camel to underscore
let newName = oldName.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
// Only process if names are different
if (newName != oldName) {
// Check for the old property name to avoid a ReferenceError in strict mode.
if (newObj.hasOwnProperty(oldName)) {
newObj[newName] = newObj[oldName];
delete newObj[oldName];
}
}
// Recursion
if (typeof(newObj[newName]) == "object") {
newObj[newName] = camelCaseKeysToSnake(newObj[newName]);
}
}
return newObj;
}
//This function will rename one property to another in place
Object.prototype.renameProperty = function (oldName, newName) {
// Do nothing if the names are the same
if (oldName == newName) {
return this;
}
// Check for the old property name to avoid a ReferenceError in strict mode.
if (this.hasOwnProperty(oldName)) {
this[newName] = this[oldName];
delete this[oldName];
}
return this;
};
//rename this to something like camelCase to snakeCase
function doStuff(object) {
for (var property in object) {
if (object.hasOwnProperty(property)) {
var r = property.replace(/([A-Z])/, function(v) { return '_' + v.toLowerCase(); });
console.log(object);
object.renameProperty(property, r);
console.log(object);
}
}
}
//example object
var camelCased = {firstName: 'Jon', lastName: 'Smith'};
doStuff(camelCased);
9条答案
按热度按时间qmelpv7a1#
下面是将camelCase转换为带下划线文本的函数(请参见jsfiddle):
然后你可以直接循环(参见other jsfiddle):
l7wslrjt2#
如果您的对象具有子对象,则可以使用递归并变更所有属性:
因此,对于这样一个对象:
您将获得:
w3nuxt5m3#
es6节点解决方案如下。要使用,需要此文件,然后将您要转换的对象传递到函数中,它将返回对象的camelcased / snakecased副本。
pdsfdshx4#
在JS和python/ruby对象之间工作时遇到了这个问题。我注意到公认的解决方案是使用
for in
,这将向您抛出eslint错误消息,参考:https://github.com/airbnb/javascript/issues/851,其中提到第11.1条规则,内容是:使用纯功能而非副作用参考:https://github.com/airbnb/javascript#iterators--nope为此,我想我会分享下面通过了上述规则。
vd8tlhqk5#
Marcos Dimitrio在上面发布了他的转换函数,它可以工作,但不是一个纯函数,因为它改变了传入的原始对象,这可能是一个不希望的副作用。下面返回一个新对象,它不修改原始对象。
rqcrx0a66#
此库的作用是:case-converter它将snake_case转换为camelCase,反之亦然
hsvhsicv7#
根据上面建议,不推荐使用
case-converter
库,请使用snakecase-keys
代替-https://github.com/bendrucker/snakecase-keys还支持嵌套对象和排除。
s1ag04yj8#
以上任何一个snakeCase函数都可以在reduce函数中使用:
mnowg1ta9#
jsfiddle(第一个字母)
***注意:****请记住删除所有
console.log
,因为生产代码不需要它们 *