根据对象值向对象添加名称

时间:2015-11-24 02:12:03

标签: javascript object

我正在根据我拥有的CSV文件生成JavaScript对象。

这是对象当前的样子 - 一个对象数组:

     [
        {
        "country": "afghanistan",
        "iso3": "afg",
        "first_characteristic": 3,
        "second_characteristic": 5,
        "third_characteristic": 3
        },
        {
        "country": "united states",
        "iso3": "usa",
        "first_characteristic": 8,
        "second_characteristic": 6,
        "third_characteristic": 7
        },
        {
        "country": "china",
        "iso3": "chn",
        "first_characteristic": 6,
        "second_characteristic": 0.7,
        "third_characteristic": 2
        }
    ]

The fiddle is here

我想为每个对象添加一个名称,该名称是从其中一个值派生的,并且输出是一个嵌套对象。

所以这就是我希望新对象的样子:

    {
        "afg":{
        "country": "afghanistan",
        "iso3": "afg",
        "first_indicator": 3,
        "second_indicator": 5,
        "third_indicator": 3
        },
        "usa":{
        "country": "united states",
        "iso3": "usa",
        "first_indicator": 8,
        "second_indicator": 6,
        "third_indicator": 7
        },
        "chn":{
        "country": "china",
        "iso3": "chn",
        "first_indicator": 6,
        "second_indicator": 0.7,
        "third_indicator": 2
        }
    }

我无法弄清楚如何添加这些名称。非常感谢任何帮助。

0 个答案:

没有答案