我有JSON响应,看起来像这样
{"values":
[
{
"type": "NAME",
"match": "EXACT",
more properties here
},
{
"type": "LASTNAME",
"match": "AMBIG",
more properties here
}
] }
如何将此转换为仅包含类型和匹配属性的javascript地图?
例如
{
'NAME' : 'EXACT' ,
'LASTNAME' : 'AMBIG'
}
答案 0 :(得分:0)
这是我的尝试:http://jsfiddle.net/u9ycpLgs/
循环遍历test.values并创建一个新对象,其中type的值是新键,匹配的值是新值。
var test = {
"values": [{
"type": "NAME",
"match": "EXACT"
}, {
"type": "LASTNAME",
"match": "AMBIG"
}]
};
keyValuePairs = {};
test.values.forEach(function (item) {
keyValuePairs[item.type] = item.match;
});
console.log(JSON.stringify(keyValuePairs));
/*
Output:
{"NAME":"EXACT","LASTNAME":"AMBIG"}
*/