繁体   English   中英

如何将json响应的一部分转换为map

[英]how to convert part of json response to map

我有看起来像这样的JSON响应

{"values": 
 [
        {
            "type": "NAME",
            "match": "EXACT",
            more properties here
        },
        {
            "type": "LASTNAME",
            "match": "AMBIG",
            more properties here
        }
] }

如何将其转换为仅包含type和match属性的javascript映射?

例如

{
 'NAME' : 'EXACT' ,
 'LASTNAME' : 'AMBIG' 
}

这是我的尝试: http : //jsfiddle.net/u9ycpLgs/

遍历test.values并创建一个新对象,其中类型的值是新键,而match的值是新值。

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"}
*/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM