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
}
]
如果不能提供所需的輸出,請給我留下了評論。 – mickmackusa
@請參閱我的更新答案,我需要這樣,以前我發佈錯誤請檢查並更新代碼 –
爲什麼你想存儲這樣的冗餘數據? – mickmackusa