如何为jvector地图创建动态json以添加国家/地区数据

时间:2019-06-18 06:39:04

标签: javascript jquery jvectormap hubspot

我需要创建以下josn

var Zone = {
   'allzones': {
     "AD":[1, '2G,3G', 1],
     "AE":[1, '2G,3G,LTE', 2],
     "AF":[1, '2G,3G', 3],
     "AG":[1, '2G,3G,LTE', 1],
     "AI":[1, '2G,3G,LTE', 2],
     "AL":[1, '2G,3G,LTE', 3],
     "AM":[1, '2G,3G,LTE', 3],
     "AO":[1, '2G,3G', 1],
     "AR":[1, '2G,3G,LTE', 2],
     "AT":[1, '2G,3G,LTE', 3],
     "AU":[1, '3G,LTE', 2],
     "AW":[1, '2G,3G', 1],
     "AX":[1, '2G,3G', 1],
     "AZ":[1, '2G,3G,LTE', 2],
     "BA":[1, '2G,3G', 2],
     "BB":[1, '2G,3G', 1],
     "BD":[1, '2G,3G,LTE', 2],
     "BE":[1, '2G,3G,LTE', 3],
     "BF":[1, '2G,3G', 1],
     "BG":[1, '2G,3G,LTE', 2],
     "BH":[1, '2G,3G,LTE', 3],
     "BI":[1, '2G,3G', 2],
     "BJ":[1, '2G,3G', 1],
     "BM":[1, '2G,3G', 1],
     "BN":[1, '2G,3G,LTE', 2],
     "BO":[1, '2G,3G,LTE', 2],
     "BQ":[1, '2G,3G', 1],
     "BR":[1, '2G,3G,LTE', 4],
     "BS":[1, '2G,3G,LTE', 2],
     "BW":[1, '2G,3G', 1],
     "BY":[1, '2G,3G,LTE', 2],
     "BZ":[1, '2G,3G,LTE', 2],
     "CA":[1, '2G,3G,LTE', 3],
     "CD":[1, '2G,3G', 5],
     "CF":[1, '2G,3G', 1],
     "CG":[1, '2G,3G', 1],
     "CH":[1, '2G,3G,LTE', 3],
     "CI":[1, '2G,3G', 1],
     "CL":[1, '2G,3G,LTE', 3],
     "CM":[1, '2G,3G', 2],
     "CN":[1, '2G,3G,LTE', 4],
     "CO":[1, '2G,3G,LTE', 2],
     "CR":[1, '2G,3G,LTE', 3],
     "CU":[1, '2G,3G', 1],
     "CV":[1, '2G,3G', 2],
     "CY":[1, '2G,3G,LTE', 3],
     "CZ":[1, '2G,3G,LTE', 3],
     "DE":[1, '2G,3G,LTE', 3],
     "DJ":[1, '2G,3G', 1],
     "DK":[1, '2G,3G,LTE', 4],
     "DM":[1, '2G,3G,LTE', 1],
     "DO":[1, '2G,3G,LTE', 1],
     "DZ":[1, '2G,3G,LTE', 3],
     "EC":[1, '2G,3G,LTE', 2],
     "EE":[1, '2G,3G,LTE', 3],
     "EG":[1, '2G,3G,LTE', 3],
     "ES":[1, '2G,3G,LTE', 4],
     "ET":[1, '2G,3G', 1],
     "FI":[1, '2G,3G,LTE', 4],
     "FJ":[1, '2G,3G', 1],
     "FO":[1, '2G,3G,LTE', 2],
     "FR":[1, '2G,3G,LTE', 4],
     "FW":[1, '2G,3G', 1],
     "GA":[1, '2G,3G', 4],
     "GB":[1, '2G,3G,LTE', 5],
     "GD":[1, '2G,3G', 1],
     "GE":[1, '2G,3G', 2],
     "GF":[1, '2G,3G', 1],
     "GH":[1, '2G,3G', 4],
     "GI":[1, '2G,3G,LTE', 1],
     "GL":[1, '2G,3G', 1],
     "GM":[1, '2G,3G', 2],
     "GN":[1, '2G,3G', 2],
     "GQ":[1, '2G,3G', 1],
     "GR":[1, '2G,3G,LTE', 4],
     "GT":[1, '2G,3G,LTE', 2],
     "GU":[1, '2G,3G', 2],
     "GY":[1, '2G,3G', 1],
     "HK":[1, '2G,3G,LTE', 4],
     "HN":[1, '2G,3G,LTE', 2],
     "HR":[1, '2G,3G,LTE', 3],
     "HT":[1, '2G,3G', 2],
     "HU":[1, '2G,3G,LTE', 3],
     "ID":[1, '2G,3G,LTE', 5],
     "IE":[1, '2G,3G,LTE', 4],
     "IL":[1, '2G,3G,LTE', 4],
     "IM":[1, '2G,3G,LTE', 1],
     "IN":[1, '2G,3G,LTE', 7],
     "IQ":[1, '2G,3G', 1],
     "IR":[1, '2G,3G,LTE', 3],
     "IS":[1, '2G,3G,LTE', 3],
     "IT":[1, '2G,3G,LTE', 4],
     "JE":[1, '2G,3G,LTE', 1],
     "JM":[1, '2G,3G,LTE', 1],
     "JO":[1, '2G,3G', 3],
     "JP":[1, '2G,3G,LTE', 3],
     "KE":[1, '2G,3G', 4],
     "KG":[1, '2G,3G', 1],
     "KH":[1, '2G,3G,LTE', 3],
     "KI":[1, '3G', 1],
     "KN":[1, '2G,3G,LTE', 1],
     "KR":[1, '2G,3G,LTE', 3],
     "KW":[1, '2G,3G,LTE', 3],
     "KY":[1, '2G,3G', 1],
     "KZ":[1, '2G,3G,LTE', 3],
     "LA":[1, '2G,3G', 2],
     "LB":[1, '2G,3G', 2],
     "LC":[1, '2G,3G,LTE', 1],
     "LI":[1, '2G,3G', 2],
     "LK":[1, '2G,3G,LTE', 4],
     "LS":[1, '2G,3G', 1],
     "LT":[1, '2G,3G,LTE', 3],
     "LU":[1, '2G,3G,LTE', 3],
     "LV":[1, '2G,3G,LTE', 2],
     "LY":[1, '2G,3G', 1],
     "MA":[1, '2G,3G,LTE', 3],
     "MC":[1, '2G,3G,LTE', 1],
     "MD":[1, '2G,3G,LTE', 2],
     "ME":[1, '2G,3G,LTE', 2],
     "MG":[1, '2G,3G', 2],
     "MK":[1, '2G,3G', 1],
     "ML":[1, '2G,3G,LTE', 1],
     "MM":[1, '2G,3G,LTE', 2],
     "MN":[1, '2G,3G', 2],
     "MO":[1, '2G,3G,LTE', 3],
     "MR":[1, '2G,3G', 1],
     "MS":[1, '2G,3G', 1],
     "MT":[1, '2G,3G,LTE', 2],
     "MU":[1, '2G,3G,LTE', 3],
     "MV":[1, '2G,3G', 2],
     "MW":[1, '2G,3G', 2],
     "MX":[1, '2G,3G,LTE', 4],
     "MY":[1, '2G,3G,LTE', 5],
     "MZ":[1, '2G,3G', 2],
     "NA":[1, '2G,3G', 1],
     "NC":[1, '2G,3G', 1],
     "NE":[1, '2G,3G', 1],
     "NG":[1, '2G,3G,LTE', 5],
     "NI":[1, '2G,3G,LTE', 3],
     "NL":[1, '2G,3G,LTE', 4],
     "NO":[1, '2G,3G,LTE', 3],
     "NP":[1, '2G,3G', 2],
     "NT":[1, '2G,3G', 3],
     "NZ":[1, '2G,3G,LTE', 3],
     "OM":[1, '2G,3G,LTE', 2],
     "PA":[1, '2G,3G,LTE', 4],
     "PE":[1, '2G,3G,LTE', 3],
     "PF":[1, '2G,3G', 1],
     "PG":[1, '2G,3G', 1],
     "PH":[1, '2G,3G,LTE', 3],
     "PK":[1, '2G,3G,LTE', 4],
     "PL":[1, '2G,3G,LTE', 4],
     "PR":[1, '2G,3G,LTE', 1],
     "PS":[1, '2G,3G', 1],
     "PT":[1, '2G,3G,LTE', 3],
     "PY":[1, '2G,3G,LTE', 3],
     "QA":[1, '2G,3G,LTE', 2],
     "RO":[1, '2G,3G,LTE', 3],
     "RS":[1, '2G,3G,LTE', 1],
     "RU":[1, '2G,3G,LTE', 5],
     "RW":[1, '2G,3G', 2],
     "SA":[1, '2G,3G,LTE', 3],
     "SC":[1, '2G,3G', 1],
     "SD":[1, '2G,3G', 1],
     "SE":[1, '2G,3G,LTE', 4],
     "SG":[1, '3G,LTE', 5],
     "SI":[1, '2G,3G,LTE', 3],
     "SK":[1, '2G,3G,LTE', 3],
     "SL":[1, '2G,3G', 2],
     "SN":[1, '2G,3G', 1],
     "SR":[1, '2G,3G', 2],
     "SS":[1, '2G,3G', 2],
     "SV":[1, '2G,3G', 4],
     "TC":[1, '2G,3G,LTE', 1],
     "TD":[1, '2G,3G', 1],
     "TG":[1, '2G,3G', 1],
     "TH":[1, '2G,3G,LTE', 5],
     "TJ":[1, '2G,3G', 1],
     "TN":[1, '2G,3G,LTE', 2],
     "TO":[1, '2G,3G', 1],
     "TP":[1, '2G,3G', 1],
     "TR":[1, '2G,3G,LTE', 3],
     "TT":[1, '2G,3G', 1],
     "TW":[1, '3G,LTE', 4],
     "TZ":[1, '2G,3G', 6],
     "UA":[1, '2G,3G', 4],
     "UG":[1, '2G,3G,LTE', 6],
     "US":[1, '2G,3G,LTE', 10],
     "UY":[1, '2G,3G,LTE', 2],
     "UZ":[1, '2G,3G,LTE', 2],
     "VC":[1, '2G,3G', 1],
     "VE":[1, '2G,3G', 1],
     "VG":[1, '2G,3G,LTE', 2],
     "VN":[1, '2G,3G', 4],
     "VU":[1, '2G,3G', 1],
     "XK":[1, '2G', 1],
     "YE":[1, '2G,3G', 1],
     "ZA":[1, '2G,3G,LTE', 4],
     "ZM":[1, '2G,3G', 1],
     "ZW":[1, '2G,3G', 2],
   }};

但是$.each返回的最后一个值
JScode

$(document).ready(function() {

        var zoneColor = {};
        var dataZone = {};

        var operators ; 
            $.getJSON("https://api.hubapi.com/hubdb/api/v1/tables/******/rows?portalId=******", function(data) {        
        $.each(data.objects, function(key, val) {

        var colorcode1  = val.values[8];
        var colorcode2  = val.values[10];
        var country     = val.values[1];
        var covergae    = val.values[12];
            operators   = val.values[7];


            zoneColor.allzones = [colorcode1, colorcode2];
            dataZone.allzones = {[country]:[1, covergae, operators]};       
            console.log(dataZone);  
        });



            var zoneID = 'allzones';

            $('#map-allzones').vectorMap({
                map: 'world_mill',
                backgroundColor: 'white',
                regionStyle: {
                    initial: {
                        fill: '#cfcccc',
                        "fill-opacity": 1,
                        stroke: 'none',
                        "stroke-width": 0,
                        "stroke-opacity": 1
                    }
                },
                series: {
                    regions: [{
                        values: dataZone[zoneID],
                        scale: zoneColor[zoneID],
                        normalizeFunction: 'polynomial'
                    }]
                },
                onRegionTipShow: function(e, el, code){
           el.html(el.html()
             +' Network - ' + dataZone[zoneID][code][1] + '</br>'
             +' Number of Operators - ' + operators);
         },

            });
    });
});

我得到的json输出:
enter image description here 我需要的json输出:
enter image description here

1 个答案:

答案 0 :(得分:2)

尝试以下操作:

var dataZone={};
 dataZone.allzones = {};//create a empty object
 $.each(data.objects, function(key, val) {

        var colorcode1  = val.values[8];
        var colorcode2  = val.values[10];
        var country     = val.values[1];
        var covergae    = val.values[12];
            operators   = val.values[7];

            dataZone.allzones[country] = [1, covergae, operators]; //appent to the object the values     

        });
       console.log(dataZone);