2017-02-26 82 views
0
{ 
    "adult": false, 
    "budget": 17000000, 
    "crew": [ 
    { 
     "credit_id": {}, 
     "department": "Directing", 
     "id": 40223, 
     "job": "Director", 
     "name": "Joe Carnahan", 
     "profile_path": "/5YPrZ1JprLwtU4tn5DG0wqLjsAT.jpg" 
    }, 
    { 
     "credit_id": "55444d6bc3a368573b0008ba", 
     "department": "Writing", 
     "id": 40223, 
     "job": "Writer", 
     "name": "Joe Carnahan", 
     "profile_path": "/5YPrZ1JprLwtU4tn5DG0wqLjsAT.jpg" 
    }, 
    { 
     "credit_id": "52fe4482c3a36847f809a3ed", 
     "department": "Production", 
     "id": 2236, 
     "job": "Producer", 
     "name": "Tim Bevan", 
     "profile_path": "/f7o93O1KocuLwIrSa7KqyL1sWaT.jpg" 
    } 
} 

嗨! 這是tmdb php api的tmdb示例輸出。 如何使用jquery獲取董事姓名例如? 船員輸出的順序是隨機的。在JSON對象中查找具體值

+0

可能重複[在JavaScript對象數組中查找對象的id](http://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of- javascript-objects) – JJJ

+0

'''var object = [your object]; var directors = []; Directors = jQuery.grep(object.crew,function(element,i){ return element.name ==='Director'; });'''如果船員中有幾個導演? – jedgard

回答

0

試試這個:

var aMovie = { 
 
    "adult": false, 
 
    "budget": 17000000, 
 
    "crew": [ 
 
    { 
 
     "credit_id": {}, 
 
     "department": "Directing", 
 
     "id": 40223, 
 
     "job": "Director", 
 
     "name": "Joe Carnahan", 
 
     "profile_path": "/5YPrZ1JprLwtU4tn5DG0wqLjsAT.jpg" 
 
    }, 
 
    { 
 
     "credit_id": "55444d6bc3a368573b0008ba", 
 
     "department": "Writing", 
 
     "id": 40223, 
 
     "job": "Writer", 
 
     "name": "Joe Carnahan", 
 
     "profile_path": "/5YPrZ1JprLwtU4tn5DG0wqLjsAT.jpg" 
 
    }, 
 
    { 
 
     "credit_id": "52fe4482c3a36847f809a3ed", 
 
     "department": "Production", 
 
     "id": 2236, 
 
     "job": "Producer", 
 
     "name": "Tim Bevan", 
 
     "profile_path": "/f7o93O1KocuLwIrSa7KqyL1sWaT.jpg" 
 
    }] 
 
}; 
 

 
var findDirector = function(aMovie){ 
 
    if(!aMovie.crew || aMovie.crew.length==0) return ""; 
 
    
 
    var director = aMovie.crew.find(function(member){ 
 
     return member.job.toLowerCase() == 'director'; 
 
    }); 
 
    return director.name; 
 
}; 
 

 
alert(findDirector(aMovie));

0

通常,當您在數組中搜索某些內容時,您希望遍歷數組直到找到它。無論您使用jQuery還是普通JavaScript或其他任何具有數組的語言,這都是一樣的。

對於你的情況,你想要搜索的屬性job等於字符串"Director"的對象。

一旦找到它,您想要返回該對象的屬性name

你可以用for循環做到這一點:

function findDirectorName(data) { 
    for (let i = 0; i < data.crew.length; i++) { 
    let crewMember = data.crew[i]; 
    if (crewMember.job === 'Director') { 
     return crewMember.name; 
    } 
    } 
} 

或許while循環:

function findDirectorName(data) { 
    let i = 0; 
    while (i < data.crew.length) { 
    let crewMember = data.crew[i]; 
    if (crewMember.job === 'Director') { 
     return crewMember.name; 
    } 
    i++; 
    } 
} 

使用內置的方法Array.prototype.find和箭頭的功能,可以簡化代碼:

function findDirectorName(data) { 
    let director = data.crew.find(crewMember => crewMember.job === 'Director'); 
    return director ? director.name : undefined; 
} 
0

我喜歡使用some()

aMovie.crew.some(function(member) { 

    var job = member.job.toLowerCase(); 

    if(job == "director") return member; 

}