簡體   English   中英

JQUERY .each如何到達對象內的數組?

[英]JQUERY .each how to reach array within object?

我有使用ajax函數獲取的JSON數據,我使用像這樣的每個函數遍歷此數據:

                    $.each(data, function (index, trip) {
                    // trip contains data
                    console.log(trip);

                    content += '<article class="tripPreview">';
                    content += '<span class="tripTitle">'+trip.title+ '</span>';
                    content += '<div class="tripOverlay">'+trip.description+ '</div>';
                    content += '<img src="public/uploads/tripphoto/'+ trip.id +'/'+trip.tripphotos.filename+'">';   
                    content += '</article>';        

                });

如何到達tripphotos > filename > thumb > url

JSON:

{"created_at":"2012-12-06T13:02:03Z","description":"test","id":11,"province":"Friesland","range_high":null,"range_low":null,"start_city":"Leeuwarden","title":"test","updated_at":"2012-12-06T13:02:06Z","user_id":1,"views":1,"categories":[{"ar_association_key_name":"4","created_at":"2012-12-04T13:12:43Z","id":2,"name":"Urban","updated_at":"2012-12-04T13:12:43Z"}],"tripphotos":[{"created_at":"2012-12-06T13:02:05Z","filename":{"url":"/uploads/tripphoto/filename/2/1280x1024-colour-wood-flip.jpg","thumb":{"url":"/uploads/tripphoto/filename/2/thumb_1280x1024-colour-wood-flip.jpg"}},"id":2,"trip_id":11,"updated_at":"2012-12-06T13:02:05Z"}]}

從您的JSON,它看起來像tripphotos是一個數組,所以你需要其他each

$.each(data, function (index, trip) {
   ...
   $.each(trip.tripphotos,function(index2,tripphoto){
      console.log(tripphoto.filename.thumb.url);
   });
});

除非您知道將永遠只有一個(或者,您只在第一個之后):

$.each(data, function (index, trip) {
    ...
    console.log(trip.tripphotos[0].filename.thumb.url);

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM