【发布时间】:2016-11-20 23:22:35
【问题描述】:
我的 lodash 代码中有一个非常奇怪的问题
我有类似的东西
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 会变成
data = {
'id':'123',
'employeeName': 'John',
'employeeType': 'new'
}
在我运行上面的代码之后,它仍然保持原样
data = {
'id':'123',
'employee_name': 'John',
'employee_type': 'new'
}
这太奇怪了,我不确定出了什么问题。有人可以帮我解决这个问题吗?非常感谢!
【问题讨论】:
-
newObj看起来像什么?另外,我认为 mapValues 会映射值,保持键不变 -
@JaromandaX { 'id':'id', 'employee_name':'employeeName', 'employ_type':'employType' } 我想我需要使用与 mapValues 不同的地图函数
-
是的,因为您将值映射到键
-
@JaromandaX 但我在切换时收到 _.mapKeys is not a function 错误消息......很奇怪。
-
是
mapKeys是您编写的函数还是您在 lodash 文档中找到的函数?
标签: javascript lodash