我有一個電影飼料,我試圖檢索具有特定鍵值的對象的「名稱」值。這裏的響應:基於嵌套數組中匹配鍵值獲取對象
{
genres: [
{
id: 18,
name: "Drama"
},
],
homepage: "http://www.therewillbeblood.com/",
id: 7345,
imdb_id: "tt0469494",
release_date: "2007-12-26",
crew: [
{
id: 4762,
name: "Paul Thomas Anderson",
department: "Writing",
job: "Screenplay",
profile_path: "/6lDE5N6lQUvAbKBRazn2Q0mRk44.jpg"
},
{
id: 52563,
name: "Upton Sinclair",
department: "Writing",
job: "Novel",
profile_path: null
},
{
id: 2950,
name: "Robert Elswit",
department: "Camera",
job: "Director of Photography",
profile_path: null
},
{
id: 1809,
name: "Dylan Tichenor",
department: "Editing",
job: "Editor",
profile_path: null
},
{
id: 4762,
name: "Paul Thomas Anderson",
department: "Directing",
job: "Director",
profile_path: "/6lDE5N6lQUvAbKBRazn2Q0mRk44.jpg"
},
{
id: 4772,
name: "Cassandra Kulukundis",
department: "Production",
job: "Casting",
profile_path: null
}
]
}
所以我試圖讓與「工作」對象「導演」這將是保羅·托馬斯·安德森的鍵值。
這裏是我的JavaScript,我設置基於release_date的值的年份變量。只是想弄清楚如何設置導演變量:
<script type="text/javascript">
$(document).ready(function() {
var url = 'http://api.themoviedb.org/3/movie/';
imdb_id = 'tt0102685';
key = '?api_key=XXXXXXXXXXXX';
append = '&append_to_response=credits';
$.ajax({
type: 'GET',
url: url + imdb_id + key + append,
dataType: 'jsonp',
success: function(data) {
var $year = data.release_date.substring(0, 4);
$('#year').html($year);
},
});
});
</script>
任何幫助將不勝感激!花了差不多兩天的時間試圖找出結果無濟於事。
工程,但應該可能有錯誤處理代碼的情況下返回的數組可能是空的。 – broofa
另外'(e.job ==「Director」)'表達式已經導致布爾值。不需要使用三元運算符來轉換爲true:false。所以只是'返回e.job ==='導演';' – broofa
@broofa好建議謝謝。 –