如何在我的应用程序中将蛇案转换为驼峰案 [英] How to convert snake case to camelcase in my app
本文介绍了如何在我的应用程序中将蛇案转换为驼峰案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的lodash代码中有一个很奇怪的问题
I have a very weird issue in my lodash codes
我有类似
data = {
'id':'123',
'employee_name': 'John',
'employee_type': 'new'
}
var newObj = _.mapValues(data, function (value, key) {
var t = _.camelCase(key);
console.log(t) -> shows employeeName and employeeType
return _.camelCase(key);
});
我期望我的newObj会成为
I was expecting my newObj will become
data = {
'id':'123',
'employeeName': 'John',
'employeeType': 'new'
}
我运行了上面的代码后,它仍然与
after I ran the codes above, it still stays the same as it was like
data = {
'id':'123',
'employee_name': 'John',
'employee_type': 'new'
}
这太奇怪了,我不确定出了什么问题.有人可以帮我吗?非常感谢!
This is super weird and I'm not sure what went wrong. Can someone help me about this? Thanks a lot!
推荐答案
使用 _.mapKeys()
代替 _.mapValues()
:
var data = {
'id': '123',
'employee_name': 'John',
'employee_type': 'new'
};
var newObj = _.mapKeys(data, (value, key) => _.camelCase(key));
console.log('newObj: ', newObj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
如果您需要忽略冗余的 value
参数,则可以使用在 _.camelCase()
上的 _.rearg()
生成一个采用第二个参数( key
)的函数第一个参数( value
)的值.
If you need to ignore the redundant value
param, you can use _.rearg()
on _.camelCase()
to generate a function that takes the 2nd param (the key
) instead of the 1st param (the value
).
var data = {
'id': '123',
'employee_name': 'John',
'employee_type': 'new'
};
var newObj = _.mapKeys(data, _.rearg(_.camelCase, 1));
console.log('newObj: ', newObj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
这篇关于如何在我的应用程序中将蛇案转换为驼峰案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文