0
下面是我用JavaScript解析的JSON數據的一部分。限制解析JSON細節時檢索到的對象數
"credits":{
"cast":[{
"id":819,"name":"Edward Norton","character":"The Narrator","order":0,"cast_id":4,"profile_path":"/iUiePUAQKN4GY6jorH9m23cbVli.jpg"
},
{"id":287,"name":"Brad Pitt","character":"Tyler Durden","order":1,"cast_id":5,"profile_path":"/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"
},
{"id":1283,"name":"Helena Bonham Carter","character":"Marla Singer","order":2,"cast_id":6,"profile_path":"/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"
},
{"id":7470,"name":"Meat Loaf","character":"Robert 'Bob' Paulson","order":3,"cast_id":7,"profile_path":"/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"
},
{"id":7499,"name":"Jared Leto","character":"Angel Face","order":4,"cast_id":30,"profile_path":"/msugySeTCyCmlRWtyB6sMixTQYY.jpg"
},
{"id":7471,"name":"Zach Grenier","character":"Richard Chesler","order":5,"cast_id":31,"profile_path":"/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"
},
{"id":7497,"name":"Holt McCallany","character":"The Mechanic","order":6,"cast_id":32,"profile_path":"/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
},
{"id":7498,"name":"Eion Bailey","character":"Ricky","order":7,"cast_id":33,"profile_path":"/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"}]
我用下面的代碼解析數據,它完美的工作。我的問題是,我想限制只有5
actor = 0,
acting = [];
for (actor = 0; actor < entries.credits.cast.length; actor++) {
acting.push(entries.credits.cast[actor].name + ' as '
+ entries.credits.cast[actor].character);
}
document.getElementById('cast').innerHTML = acting.join(', ');
;演員<條目。 credits.cast.length; actor ++){'to'for(actor = 0; actor <5; actor ++){'將從列表中拉出前五個。 –
@RyanWillis,但是當少於5時出現錯誤,所以'min(5,entries.credits.cast.length)'會比'5'更好0 –
我的不好,然後緊接着'acting = [] ;'你可以有'actlimit =(entries.credits.cast.length <5)? entries.credits.cast.length:5;'並將'5'改爲'actlimit' –