jsonformatresponsejqvmap

Customize Json Response


I'm trying to implement jqvmap to my project. My json response in original like;

[

{
    "Count": 10,
    "ProvinceCode": 34
},
{
    "Count": 6,
    "ProvinceCode": 59
}

The format below, guess only format that jqvmaps accepts.

var sample_data = {"34":"10","59":"6"};

I've tried some other methods to create response like that but not any success. Any way to create response like that?


Solution

  • Try this it will work :

    var newObj = {};
    var obj = [{
        "Count": 10,
        "ProvinceCode": 34
    }, {
        "Count": 6,
        "ProvinceCode": 59
    }];
    
    for (var item in obj) {
    var keys = obj[item].ProvinceCode;
    var values = obj[item].Count;
    newObj[keys] = values;
    }
    
    console.log(newObj);
    

    Working fiddle : https://jsfiddle.net/s3x2ngdv/

    Output :

    enter image description here