JSON解析数组循环

时间:2017-12-07 16:42:18

标签: javascript arrays json

我正在尝试将我的数组解析为代表这样的数据

-1.80511,53.84632:-1.80511,53.84632:-1.80511,53.84632:xxx,xxx:xxx
 lat,lng:lat,lng

这样它就可以被api读取。这是我到目前为止的代码,我只能把它变成这种格式(coordsnew)

-1.7651,53.88566,-1.76896,53.8866,-1.77125,53.88801

我当前的代码

    var coordsnew = [];
    var i;
    for (i = 0; i < coords.length; i++) {
     coordsnew += coords[i];
                console.log(coordsnew);

我的coords数组数据目前是这样形成的

     var statesdata = {
"type": "FeatureCollection",

"features": [
{ "type": "Feature", "properties": { "Name": "BD1 1", "Description": "BD1 1 postcode sector<br  \/><a href=\"http:\/\/www.doogal.co.uk\/UKPostcodes.php?Search=BD1 1\" >Details<\/a>" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -1.7526003839732, 53.795297076485198 ], [ -1.75078461615826, 53.795607063948303 ], [ -1.75078061651426, 53.795604064354201 ], [ -1.75051864373599, 53.795500080054303 ], [ -1.74977069342604, 53.794539203056601 ], [ -1.74974469120328, 53.794412218350601 ], [ -1.7492607290865, 53.793926281950498 ], [ -1.74918471905401, 53.793471336517399 ], [ -1.74939968041321, 53.793170369115202 ], [ -1.74925067188821, 53.792542445011001 ], [ -1.74902869183461, 53.792380467011299 ], [ -1.74897763675983, 53.790929638498604 ], [ -1.74976552406105, 53.790511677309198 ], [ -1.75025545811429, 53.790349689915303 ], [ -1.75108238214196, 53.790913612609998 ], [ -1.75116539246416, 53.791395554773104 ], [ -1.75217230177679, 53.7921254555514 ], [ -1.75238127314532, 53.792044462329898 ], [ -1.75393711035648, 53.792635372231103 ], [ -1.75434805085846, 53.792400394475202 ], [ -1.75315716556359, 53.791713491060101 ], [ -1.7536130920749, 53.791275536607898 ], [ -1.75509590228284, 53.7910175474209 ], [ -1.75604581420275, 53.791646460844603 ], [ -1.75617780646011, 53.791840436265602 ], [ -1.75572287565107, 53.792179402357696 ], [ -1.75536192519302, 53.792321390401902 ], [ -1.75511998619384, 53.793075304829799 ], [ -1.75459505898236, 53.793299285383803 ], [ -1.75372115084327, 53.792977334818701 ], [ -1.75370116224194, 53.793190310007297 ], [ -1.75374816559962, 53.793404284194501 ], [ -1.75376716436202, 53.793429281000698 ], [ -1.75431110539553, 53.793587255224999 ], [ -1.75449608126177, 53.793544257847103 ], [ -1.75468606209324, 53.793633244863699 ], [ -1.75451310097882, 53.794060196877098 ], [ -1.75454309938432, 53.794108190830698 ], [ -1.75468508313808, 53.794129186485499 ], [ -1.75508202351291, 53.7938512139769 ], [ -1.75526600519453, 53.793943200719497 ], [ -1.75589992558159, 53.793869201069697 ], [ -1.75611888768616, 53.7935972302026 ], [ -1.75644384581814, 53.793534233333297 ], [ -1.75653285692984, 53.794052171178201 ], [ -1.75652885758116, 53.794056170760101 ], [ -1.75585494164085, 53.794121171996501 ], [ -1.75585194229808, 53.794128171212002 ], [ -1.75542100357864, 53.794348150996697 ], [ -1.75538800907876, 53.794384147193803 ], [ -1.75516103908185, 53.794446142888802 ], [ -1.7546371262232, 53.795013083049803 ], [ -1.75439915177408, 53.794938095018303 ], [ -1.75387121644832, 53.794961099275 ], [ -1.75351226756764, 53.795146082231298 ], [ -1.75297532691447, 53.795017104501397 ], [ -1.75291433127843, 53.794946113664501 ], [ -1.75265236634886, 53.795028107467203 ], [ -1.7526003839732, 53.795297076485198 ] ] ], [ [ [ -1.7524052922723, 53.792566400562002 ], [ -1.75210831392775, 53.7922304440348 ], [ -1.75197033209524, 53.792266441617201 ], [ -1.75092249848606, 53.793213343959003 ], [ -1.75151443343543, 53.793364318372703 ], [ -1.75168241131046, 53.793320321336203 ], [ -1.75193937541024, 53.7932043316018 ], [ -1.7524052922723, 53.792566400562002 ] ] ] ] } }

这是数据的传递方式以及它在控制台中的表示方式

        var coords = layer.feature.geometry.coordinates;
0:Array(1)
0:Array(94)
0:Array(2) [-1.75937311731598, 53.808343451011]
 0:-1.75937311731598
 1:53.808343451011

1 个答案:

答案 0 :(得分:0)

获取所需的字符串:

 var colonString = statesdata
   .features[0]
   .properties
   .geometry
   .coordinates[0][0]
   .map(pair => pair.reverse().join())
   .join(':');

这是一些深度嵌套的geoJSON。