使用值JSON交换密钥 [英] Swap key with value JSON

查看:136
本文介绍了使用值JSON交换密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的JSON对象,结构如下:

I have an extremely large JSON object structured like this:

{A : 1, B : 2, C : 3, D : 4}

我需要一个可以用我的对象中的键交换值的函数不知道怎么做。我需要这样的输出:

I need a function that can swap the values with keys in my object and I don't know how to do it. I would need an output like this:

{1 : A, 2 : B, 3 : C, 4 : D}

我有什么方法可以手动创建一个交换所有东西的新对象吗? >
谢谢

Is there any way that I can do this would manually created a new object where everything is swapped?
Thanks

推荐答案

function swap(json){
  var ret = {};
  for(var key in json){
    ret[json[key]] = key;
  }
  return ret;
}

此处示例 FIDDLE 不要忘记打开你的控制台来查看结果。

Example here FIDDLE don't forget to turn on your console to see the results.

这篇关于使用值JSON交换密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆