【问题标题】:How can i get the length of a responsable URL in javascript如何在 javascript 中获取负责任 URL 的长度
【发布时间】:2017-03-16 15:50:10
【问题描述】:

我需要JSONOBJECT.routingpath.coordinates.length 的长度 我收到了这个网址的回复。

http://193.70.60.44:3000/taxi_server/api/v1.0/taxiroute

来自服务器的响应 ...

[{
    "idtaxiroute": "3",
    "routingpath": {
        "type": "LineString",
        "coordinates": [
            [49.9670749, 7.8994591],
            [49.9682897, 7.898872900000001],
            [49.9682897, 7.898872900000001],
            [49.9680662, 7.897596899999999],
            [49.9680662, 7.897596899999999],
            [49.9689168, 7.897404499999999],
            [49.9689168, 7.897404499999999],
            [49.9690376, 7.895451],
            [49.9690376, 7.895451],
            [49.9688003, 7.891565000000001],
            [49.9688003, 7.891565000000001],
            [49.966386, 7.888530899999999],
            [49.966386, 7.888530899999999],
            [49.9651325, 7.8884534],
            [49.9651325, 7.8884534],
            [49.9411728, 7.912222499999999],
            [49.9411728, 7.912222499999999],
            [49.9391022, 7.9152532],
            [49.9391022, 7.9152532],
            [49.9337381, 7.9157891],
            [49.9337381, 7.9157891],
            [49.9241653, 7.829844099999999],
            [49.9241653, 7.829844099999999],
            [49.9259564, 7.8275102],
            [49.9259564, 7.8275102],
            [49.922877, 7.8259417],
            [49.922877, 7.8259417],
            [49.92355939999999, 7.8055037],
            [49.92355939999999, 7.8055037],
            [49.9258713, 7.8004553],
            [49.9258713, 7.8004553],
            [49.9262293, 7.7978919],
            [49.9262293, 7.7978919],
            [49.92596469999999, 7.795387600000001],
            [49.92596469999999, 7.795387600000001],
            [49.9216437, 7.7577913],
            [49.9216437, 7.7577913],
            [49.91755209999999, 7.7473702],
            [49.91755209999999, 7.7473702],
            [49.9107566, 7.726173299999999],
            [49.9107566, 7.726173299999999],
            [49.9115306, 7.725327200000001]
        ]
    },
    "taxiid": 551,
    "riderequestid": 7
}, {
    "idtaxiroute": "4",
    "routingpath": {
        "type": "LineString",
        "coordinates": [
            [49.9670749, 7.8994591],
            [49.9670378, 7.899477099999999],
            [49.9670378, 7.899477099999999],
            [49.9651449, 7.898171800000001],
            [49.9651449, 7.898171800000001],
            [49.9645179, 7.894035400000001],
            [49.9645179, 7.894035400000001],
            [49.9605222, 7.893961699999999],
            [49.9605222, 7.893961699999999],
            [49.95326799999999, 7.902490999999999],
            [49.95326799999999, 7.902490999999999],
            [49.9518747, 7.9065943],
            [49.9518747, 7.9065943],
            [49.9495675, 7.907795699999999],
            [49.9495675, 7.907795699999999],
            [49.9479066, 7.9125997],
            [49.9479066, 7.9125997],
            [49.9405005, 7.910934200000001],
            [49.9405005, 7.910934200000001],
            [49.9400513, 7.9114172]
        ]
    },
    "taxiid": 551,
    "riderequestid": 7
}]

代码:

for(var i = 0; i < JSONOBJECT[i].routingpath.length; i++)
   {
       console.log("Type: " + JSONOBJECT[i].routingpath.coordinates[i][i]); 
    console.log("Types: " + i); 
   }

你能帮帮我吗?

【问题讨论】:

  • 您的具体要求是什么...?
  • 我想要一个带有坐标的循环并在 openlayers 中绘制一条折线,为此我需要坐标的长度来获得循环的长度

标签: javascript json loops xmlhttprequest


【解决方案1】:

在计算使用ifor循环的限制时,使用JSONOBJECT[i]是没有意义的。

您需要嵌套循环,一个用于JSONOBJECT 数组,另一个用于coordinates

for (var i = 0; i < JSONOBJECT.length; i++) {
    var coords = JSONOBJECT[i].routingpath.coordinates;
    for (var j = 0; j < coords.length; j++) {
        console.log("Type: " + coords[j]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 2017-02-14
    • 2018-08-16
    相关资源
    最近更新 更多