我目前有一些代碼顯示一些基於MySQL查詢的HTML。在我當前的查詢中,我只用MAX(ID)
(每個數組元素中的第一項)選擇不同的行。我期待現在就展開和我的數組現在看起來是這樣的:在for循環中的特定元素的Javascript數組組合
var fullcheckins = [["3835","101 Pub","40.8684","-74.0223"],
["2182","101 Pub","40.8684","-74.0223"],
["2181","101 Pub","40.8684","-74.0223"],
["1574","101 Pub","40.8684","-74.0223"],
["1573","101 Pub","40.8684","-74.0223"],
["1572","101 Pub","40.8684","-74.0223"],
["1269","101 Pub","40.8684","-74.0223"],
["1230","101 Pub","40.8684","-74.0223"],
["221","101 Pub","40.8684","-74.0223"],
["220","101 Pub","40.8684","-74.0223"],
["697","113th St. Beach, LBI","39.5938","-74.2145"],
["2838","14th Star Brewery and Taproom","44.8145","-73.0817"],
["2844","14th Star Brewing Co","44.8201","-73.0854"],
["2842","14th Star Brewing Co","44.8201","-73.0854"],
["2841","14th Star Brewing Co","44.8201","-73.0854"],
["2840","14th Star Brewing Co","44.8201","-73.0854"]];
我想是某種方式給每一行分開哪里哪里在fullcheckins每個元素是相同的,他們組合在一起。如果有幫助,陣列是有序的。以下是我期待:
for (var i = 0; i < checkins.length; i++) {
var checkin = checkins[i];
var htmlscript = '<div id="slideShowImages">'
for each time checkin[2] is the same:
htmlscript += '<img src="pictures/'+checkin[0]+'.jpg"/>'
htmlscript += '</div>'
最後,我應該通過對循環多次使用多了htmlscript
變量,像這樣:
htmlscript = '<div id="slideShowImages">
<img src="pictures/3835.jpg"/>
<img src="pictures/2182.jpg"/>
<img src="pictures/2181.jpg"/>
<img src="pictures/1574.jpg"/>
<img src="pictures/1573.jpg"/>
<img src="pictures/1572.jpg"/>
<img src="pictures/1269.jpg"/>
<img src="pictures/1230.jpg"/>
<img src="pictures/221.jpg"/>
<img src="pictures/220.jpg"/>
</div>'
有沒有辦法做到這一點?
這實際上可能非常有用。我可以在兩個元素而不是一個元素上進行組合嗎?另外,正如問題中所述,fullcheckins數組已經通過checkin [1],checkin [2],checkin [3],然後是降序checkin [0] – rjbogz
排序。您當然可以,只需更改'var groupBy無論你喜歡什麼,都可以。例如,像'var groupBy = checkin [2] +「,」+ checkin [3];''會給你看起來像''40.8684,-74.0223「' – Hamms
的密鑰我會指出沒有任何可用選項是唯一標識商業位置的完美方式(我假設您正在嘗試執行此操作)。如果有任何方法,您的查詢可以返回數據庫用於位置的任何唯一標識符,那絕對是使用的最佳關鍵。 – Hamms