有没有办法使用 underscore.js 重命名 js 对象键
我需要将 js 对象转换为另一个对象,以便传递到服务器帖子,其中键的名称不同,例如
var a = {
name : "Foo",
amount: 55,
reported : false,
...
<snip/>
...
date : "10/01/2001"
}
需要转换为
a = {
id : "Foo",
total : 55,
updated: false,
...
<snip/>
...
issued : "10/01/2001"
}
可用于映射所有键的查找 obj
var serverKeyMap = {
name : "id",
amount : "total",
reported : "updated",
...
date : "issue"
}
是否有 underscore.js 或中可用的函数我可以使用 jQuery 来实现此功能吗?
谢谢
I need to convert a js object to another object for passing onto a server post where the names of the keys differ for example
var a = {
name : "Foo",
amount: 55,
reported : false,
...
<snip/>
...
date : "10/01/2001"
}
needs to turn into
a = {
id : "Foo",
total : 55,
updated: false,
...
<snip/>
...
issued : "10/01/2001"
}
where I have lookup obj available for mapping all the keys
var serverKeyMap = {
name : "id",
amount : "total",
reported : "updated",
...
date : "issue"
}
Is there a function available in underscore.js or jQuery that I can use that does this functionality?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我知道您没有提到 lodash 并且答案已经解决了问题,但其他人可能会利用替代方案。
正如 @CookieMonster 在评论中提到的,您可以使用
_.mapKeys
来完成此操作:还有小提琴: http://jsfiddle.net/cwkwtgr3/
I know you didn't mention lodash and the answers already solve the problem, but someone else might take advantage of an alternative.
As @CookieMonster mentioned in the comments, you can do this with
_.mapKeys
:And the fiddle: http://jsfiddle.net/cwkwtgr3/
与@pimvdb类似,您也可以使用
_.reduce
来完成:Fiddle:http://jsfiddle.净/T9Lnr/39/
Similar to @pimvdb, you can also do it with a
_.reduce
:Fiddle: http://jsfiddle.net/T9Lnr/39/
据我所知,这两个库中都没有内置函数。不过,您可以相当轻松地制作自己的:http://jsfiddle.net/T9Lnr/1/。
As far as I know there is no function built into either of these two libraries. You can make your own fairly easily, though: http://jsfiddle.net/T9Lnr/1/.
您可以使用标准 JavaScript 将值复制到新属性,并使用 omit 删除原始属性,如下所示:
You could copy the values to the new properties with standard JavaScript, and remove the original properties with omit, as follows:
这可能比上面的答案慢。
This may be slower than above answers.
不,这两个库中都没有显式重命名键的函数。您的方法也是最快的(请参阅 jsperf 测试。)如果可能的话,您最好的选择是重构客户端端或服务器端代码,因此对象是相同的。
No there is no function in either library that explicitly renames keys. Your method is also the fastest (see jsperf tests.) Your best bet, if possible, is to refactor either the client side or server side code so the objects are the same.
我有一个转换运算符,只想将其应用于所有键。我分叉了 pimvdb 的小提琴来生成一个简单的示例。在这种情况下,它将键大写。它动态构建键盘映射,我需要确保它有效(感谢 JSFiddle)。
这是更改后的代码:
Fiddle:
http://jsfiddle.net/mr23/VdNjf/
I have a transformation operator and would just like to apply it to all keys. I forked pimvdb's fiddle to produce a simple example. In this case it Capitalizes the key. And it dynamically builds the keymap, which I needed to assure works (thanks JSFiddle).
Here is the changed code:
Fiddle:
http://jsfiddle.net/mr23/VdNjf/
它已在这里解决 https://stackoverflow.com/a/30940370/1360897
以及旧值和新值的映射,像这样
然后使用_.map和_.transform,你可以变换对象,像这样
It's been solved here https://stackoverflow.com/a/30940370/1360897
and also a mapping of old and new values, like this
And then using _.map and _.transform, you can transform the object, like this
为什么不使用这个简单的 java 脚本呢?任何键:值对的值应该是字符串/数字/布尔值。
Why don't you use this simple java script ? Value of any key:value pair should be string/number/Boolean.
正如 user2387823 所说的上面
As user2387823 was saying above ???? using omit is a great option. For example you could write something like this
这个ES2015/2017版本
this ES2015/2017 version ????♂️
你真的不需要下划线/lodash......无论如何现在(我意识到这个问题是9年前提出的,但是这个问题(仍然)在搜索结果中排名很高,我今天遇到了它:-))
这是我喜欢的另一个简单的 ES2015/2017 版本,受到 @malbarmavi 的回答的启发(可能还有很多其他简单的 JS 函数,但我在简短的搜索中没有遇到任何其他函数):
You really don't need underscore/lodash for this ... nowadays anyways (I realize the question was asked 9 years ago, but this question is (still) ranked highly in search results and I came across it today :-) )
Here's another plain ES2015/2017 version that I like, inspired by @malbarmavi's answer (there's probably a bunch of other plain JS functions out there, but I didn't come across any others in my brief search):
我参考了 lodash 文档并找到了
mapKeys
https://lodash.com/docs/4.17.15#mapKeys
这完美地重命名键并返回一个包含修改后的所需对象的对象
I referred the lodash documentation ans found
mapKeys
https://lodash.com/docs/4.17.15#mapKeys
this perfectly renames the keys and return an object containing the modified desirable object
使用下划线
省略
和扩展运算符。扩展运算符下面的键分配会加载新键并忽略旧键。
Using underscore
omit
and spread operator.Key assignments below spread operator loads new keys and omit omits the old ones.
您可以创建新的自定义函数:
或者如果您只想编辑一个 keyName:
You can create your new custom function :
Or else if you want to edit only one keyName:
使用lodash,
因此在您的情况下,您希望将 serverKeyMap 应用于对象 a :
来自 https ://www.geeksforgeeks.org/lodash-_-renamekeys-method/
Using lodash
so in your case, you want to apply the serverKeyMap onto object a :
from https://www.geeksforgeeks.org/lodash-_-renamekeys-method/