我的 JSON 数据如下所示:
[{"name":"age","value":31},
{"name":"height (inches)","value":62},
{"name":"location","value":"Boston, MA"},
{"name":"gender","value":"male"}]
我需要它看起来像这样:
[{"age": 31},
{"height (inches)": 62},
{"location": "Boston, MA"},
{"gender": "male"}]
这是一个 jQuery Web 应用程序。我将如何进行此转换?对如何完成并不挑剔,只是很难找到现有的解决方案。谢谢!
最佳答案
这应该可以解决问题:
a = [{"name":"age","value":31},
{"name":"height (inches)","value":62},
{"name":"location","value":"Boston, MA"},
{"name":"gender","value":"male"}];
b = a.map(function(item){
var res = {};
res[item.name] = item.value;
return res;
});
console.log(b);
关于Javascript - 转换名称 :ItemName/Value:ItemValue Objects to Regular ItemName:ItemValue JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29926677/