2017-08-14 66 views
1

使用推正常工作我有一個包含pickUpLatitidute & pickUpLongitude JSON響應,我想這些值推到一個數組waypts所以我嘗試了for迴路。For循環不同時的jquery

for循環內我推pickUpLatitidute & pickUpLongitude,但它不工作,因爲我需要。

點擊提交按鈕,你會看到我作爲迴應得到什麼。

我得到最後的pickUpLatitidute & pickUpLongitude值。

$('#btn-submit').click(function(e){ 
 
    e.preventDefault(); 
 
    var res={ 
 
     "status": "success", 
 
     "count": 3, 
 
     "data": 
 
      [ 
 
       { 
 
        "tripId": "1", 
 
        "pickUpLatitidute": "12.9565226", 
 
        "pickUpLongitude": "77.70730989999993" 
 
       }, 
 
       { 
 
        "tripId": "1", 
 
        "pickUpLatitidute": "12.9550587", 
 
        "pickUpLongitude": "77.68279819999998" 
 
       }, 
 
       { 
 
        "tripId": "1", 
 
        "pickUpLatitidute": "12.9824", 
 
        "pickUpLongitude": "77.6927990" 
 
       } 
 
      ] 
 
     }; 
 
    $.each(res['data'], function(key, value) { 
 
     if(res['status']=='success'){ 
 
      calculateAndDisplayRoute(1, 1); 
 
      // calculateAndDisplayRoute() START HERE 
 
      function calculateAndDisplayRoute(directionsService, directionsDisplay) { 
 
       var latitdute = JSON.parse(value.pickUpLatitidute); 
 
       var longitude = JSON.parse(value.pickUpLongitude); 
 
       var waypts = []; 
 
       for (i = 0; i < 3; i++) { 
 
        waypts.push(
 
         { 
 
          location: { 
 
           lat: latitdute, 
 
           lng: longitude 
 
          }, 
 
          stopover: true 
 
         } 
 
        ); 
 
       } 
 
       console.log(waypts); 
 
      } 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<form id="testForm"> 
 
    <select name="tripId"> 
 
     <option value="1">1</option> 
 
     <option value="2">2</option> 
 
    </select> 
 
    <button type="button" id="btn-submit">submit</button> 
 
</form>

我預期的結果:

[ 
    { 
     "location": { 
      "lat": 12.9565226, 
      "lng": 77.70730989999993 
     }, 
     "stopover": true 
    }, 
    { 
     "location": { 
      "lat": 12.9550587, 
      "lng": 77.68279819999998 
     }, 
     "stopover": true 
    }, 
    { 
     "location": { 
      "lat": 12.9824, 
      "lng": 77.692799 
     }, 
     "stopover": true 
    } 
] 
[ 
    { 
     "location": { 
      "lat": 12.9565226, 
      "lng": 77.70730989999993 
     }, 
     "stopover": true 
    }, 
    { 
     "location": { 
      "lat": 12.9550587, 
      "lng": 77.68279819999998 
     }, 
     "stopover": true 
    }, 
    { 
     "location": { 
      "lat": 12.9824, 
      "lng": 77.692799 
     }, 
     "stopover": true 
    } 
] 
[ 
    { 
     "location": { 
      "lat": 12.9565226, 
      "lng": 77.70730989999993 
     }, 
     "stopover": true 
    }, 
    { 
     "location": { 
      "lat": 12.9550587, 
      "lng": 77.68279819999998 
     }, 
     "stopover": true 
    }, 
    { 
     "location": { 
      "lat": 12.9824, 
      "lng": 77.692799 
     }, 
     "stopover": true 
    } 
] 

回答

1

for循環嵌套您each循環內,並且你重新聲明每次迭代waypts爲空數組。

我已將您的var waypts=[];聲明移至each()之外,並完全刪除了您的內部for循環。

$('#btn-submit').click(function(e){ 
 
    e.preventDefault(); 
 
    var res={ 
 
     "status": "success", 
 
     "count": 3, 
 
     "data": 
 
      [ 
 
       { 
 
        "tripId": "1", 
 
        "pickUpLatitidute": "12.9565226", 
 
        "pickUpLongitude": "77.70730989999993" 
 
       }, 
 
       { 
 
        "tripId": "1", 
 
        "pickUpLatitidute": "12.9550587", 
 
        "pickUpLongitude": "77.68279819999998" 
 
       }, 
 
       { 
 
        "tripId": "1", 
 
        "pickUpLatitidute": "12.9824", 
 
        "pickUpLongitude": "77.6927990" 
 
       } 
 
      ] 
 
     }; 
 
    if(res['status']=='success'){ 
 
     var waypts=[]; 
 
     for (i=0; i<res['count']; ++i) { 
 
      var temp=[]; 
 
      $.each(res['data'], function(key, value) { 
 
       calculateAndDisplayRoute(1, 1); 
 
       // calculateAndDisplayRoute() START HERE 
 
       function calculateAndDisplayRoute(directionsService, directionsDisplay) { 
 
        var latitdute = JSON.parse(value.pickUpLatitidute); 
 
        var longitude = JSON.parse(value.pickUpLongitude); 
 
        temp.push(
 
         { 
 
          location: { 
 
           lat: latitdute, 
 
           lng: longitude 
 
          }, 
 
          stopover: true 
 
         } 
 
        ); 
 
       } 
 
      }); 
 
      waypts.push(temp); 
 
     } 
 
     console.log(waypts); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<form id="testForm"> 
 
    <select name="tripId"> 
 
     <option value="1">1</option> 
 
     <option value="2">2</option> 
 
    </select> 
 
    <button type="button" id="btn-submit">submit</button> 
 
</form>

+0

如果不能提供所需的輸出,請給我留下了評論。 – mickmackusa

+0

@請參閱我的更新答案,我需要這樣,以前我發佈錯誤請檢查並更新代碼 –

+0

爲什麼你想存儲這樣的冗餘數據? – mickmackusa