我正在尝试编写一个嵌套数组,从一系列jquery .get()调用开始,基于类似json的数组(myHotels)。
function retrieveRates () {
var myHotels =[{"srvId":"995805","hotelCountry":"GREECE","hotelCity":"45220","hotelName":"HOTEL EUROPA","starLevel":"Standard ***"},{"srvId":"106442","hotelCountry":"GREECE","hotelCity":"10514","hotelName":"ATHENS","starLevel":"Superior ****"},{"srvId":"925765","hotelCountry":"GREECE","hotelCity":"10794","hotelName":"WINSTON","starLevel":"Luxury *****"},{"srvId":"922185","hotelCountry":"GREECE","hotelCity":"10514","hotelName":"HOTEL LIBERATIN","starLevel":"Superior ****"},{"srvId":"15134","hotelCountry":"GREECE","hotelCity":"10514","hotelName":"HOTEL GRILLON","starLevel":"Luxury *****"},{"srvId":"275785","hotelCountry":"GREECE","hotelCity":"10794","hotelName":"MERIDIEN","starLevel":"Luxury *****"},{"srvId":"89302","hotelCountry":"GREECE","hotelCity":"10384","hotelName":"SAVOY","starLevel":"Luxury *****"},{"srvId":"229986","hotelCountry":"GREECE","hotelCity":"10384","hotelName":"PANAKOS","starLevel":"Superior ****"},{"srvId":"735385","hotelCountry":"GREECE","hotelCity":"10674","hotelName":"RUBEIRA","starLevel":"Luxury *****"}];
resultsArray = [];
for(var i = 0; i < myHotels.length; i++) {
var obj = myHotels[i];
console.log(obj.srvId)
var propertyArray = [];
propertyArray.push(obj.srvId);
$.get('http://myUrl/getRoomTypes/'+obj.srvId+'?&CityId='+obj.hotelCity+'&HotelId='+obj.srvId+'&fromDateFormatted=2016-03-16&toDateFormatted=2016-03-18&numberOfNights=2&adults%5B0%5D=2&children%5B0%5D=0&boardBasis%5B0%5D=-1&residence%5B0%5D=6&nationality%5B0%5D=6&propertyType=&starRating=&perPage=10¤cy=413&availability=-1&customerId=87974&allowedSingleWithChildren=1&itineraryCode=&bookedItnNumber=&customerCountry=6&passengerNationalityOrCountry=3', function(data){
$(data).find('.booking-selection-row').each(function () {
var roomArray = [];
var roomDat = $(this).find('label').text();
var roomName = roomDat.split(/\[(.*?)\]/)[0];
var findProvider = function () {
providerName = "";
if (! roomDat.split(/\[(.*?)\]/)[1] ) {
return providerName = "Avenues";
}
else {
return providerName = roomDat.split(/\[(.*?)\]/)[1];
}
}
var deadLine = $.trim($(this).find('span.glyphicon.glyphicon-ok-circle').text());
roomArray.push(roomName, findProvider(), deadLine);
$(this).find('.pricePerDay table tbody tr').each(function () {
var dateRateArray = [];
var thisDate = $(this).find('td').eq(0).text();
var thisRate = $(this).find('td').eq(1).text();
var thisBoard = $(this).find('td').eq(4).first('div').text();
var thisAvail = $(this).find('td').eq(5).first('label').text();
dateRateArray.push(thisDate , thisRate , thisBoard , thisAvail);
roomArray.push(dateRateArray);
});
propertyArray.push(roomArray);
});
resultsArray.push(propertyArray);
}, "html");
};
};
retrieveRates();
输出是我想要的,除了在resultArray中,在每个对象的位置[0],我希望每次相关的obj.srvId都能看到。现在,数组总是以这种方式写入最后一个对象(“735385”,“hotelCountry”:“GREECE”)的srvId:
[["735385", ["STANDARD ROOM", "Avenues", "Deadline: Sun, 13 Mar 2016 16:59:59", 2 more...], ["STANDARD ROOM ", "BRITHotels", "Deadline: Sun, 13 Mar 2016 03:00:00", 2 more...], 194 more...], ["735385", ["STANDARD ROOM", "Avenues", "Deadline: Sun, 13 Mar 2016 16:59:59", 2 more...], ["STANDARD ROOM ", "BRITHotels", "Deadline: Sun, 13 Mar 2016 03:00:00", 2 more...], 194 more...], ["735385", ["STANDARD ROOM", "Avenues", "Deadline: Sun, 13 Mar 2016 16:59:59", 2 more...], ["STANDARD ROOM ", "BRITHotels", "Deadline: Sun, 13 Mar 2016 03:00:00", 2 more...], 194 more...], ["735385", ["STANDARD ROOM", "Avenues", "Deadline: Sun, 13 Mar 2016 16:59:59", 2 more...],
虽然每次都应该有不同的obj.srvId。
如何正确地将obj.srvId推送到数组中?就好像变量无法传递给get()成功函数。
提前感谢您的帮助。
答案 0 :(得分:0)
将$ .get等异步函数放入循环时要小心。
这里最有可能发生的是你的程序在任何$ .get调用完成之前循环遍历你的MyHotels数组。你需要做的是使用$ .get中的成功回调函数迭代你的MyHotels数组而不是for循环。