我正在根据我拥有的CSV文件生成JavaScript对象。根据对象值将名称添加到对象
这里是对象当前如下like--对象的数组:
[
{
"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
}
]
我想为每个对象添加一个名称,该名称是从其中一个值派生的,并且输出为嵌套对象。
原来这就是我想要的新对象的样子:
{
"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
}
}
我无法弄清楚如何添加这些名字。任何帮助是极大的赞赏。
请注意'[富数据: bar,fizz:buzz]在_JavaScript_中不是有效的_Array_。你是说你想要索引和键吗?只是钥匙?只是标记,但标签以某种方式连接? –
@Paul S.更新 – sprucegoose
您的目标json看起来不正确,因为数组中的每个元素的格式为“”...“:{...}'。是否需要它是一个数组,它可能是一个对象吗? – Romski