我的 JSON 响应如下所示
{"values":
[
{
"type": "NAME",
"match": "EXACT",
more properties here
},
{
"type": "LASTNAME",
"match": "AMBIG",
more properties here
}
] }
如何将其转换为仅包含类型和匹配属性的 JavaScript 映射?
例如
{
'NAME' : 'EXACT' ,
'LASTNAME' : 'AMBIG'
}
最佳答案
这是我的尝试:http://jsfiddle.net/u9ycpLgs/
循环遍历 test.values 并创建一个新对象,其中类型的值是新键,匹配的值是新值。
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"}
*/
关于javascript - 如何将部分json响应转换为map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28907773/