[英]Get object based on matching key value in nested array
I have a movie feed where I'm trying to retrieve the "name" value of an object that has a specific key value. 我有一个电影供稿,试图在其中检索具有特定键值的对象的“名称”值。 Here's the response:
这是回应:
{
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
}
]
}
So I'm trying to get the object with the "job" key value of "Director" which would be Paul Thomas Anderson. 因此,我试图使用“导演”的“工作”键值作为对象,该对象将是Paul Thomas Anderson。
And here's my javascript where I'm setting the year variable based on the value of release_date. 这是我的JavaScript,其中我根据release_date的值设置了年份变量。 Just trying to figure out how I can set a director variable:
只是想弄清楚如何设置导向变量:
<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>
Any help would be greatly appreciated! 任何帮助将不胜感激! Spent almost two days trying to figure it out to no avail.
花了将近两天时间试图弄清楚,但无济于事。
If your using modern browsers you can use the filter function: 如果您使用的是现代浏览器,则可以使用过滤器功能:
var name = data.crew.filter(function(e){
return (e.job=="Director") ? true:false;
})[0].name;
JS Fiddle: http://jsfiddle.net/gTzyT/ JS小提琴: http : //jsfiddle.net/gTzyT/
Example with Error Handling 错误处理示例
function getDirectorName(crew){
var name = "";
try{
name = json.crew.filter(function(e){
return e.job=="Director"
})[0].name;
}catch(err){
name = "No Director";
}
return name;
}
//calling the method
alert(getDirectorName(data.crew));
For a slightly "different" approach, sort by job first. 对于稍微“不同”的方法,请首先按作业排序。 Eg
例如
data.crew.sort(function(a,b) {
return a.job == 'Director' ? -1 : 1;
});
console.log(data.crew[0].name); // => name of Director
Note: I'm not necessarily endorsing this solution. 注意:我不一定支持此解决方案。 Sorting is not the most performant approach.
排序不是最有效的方法。 And it has side-effects (changes the order of the
data.crew
array, which may not be desirable). 而且它具有副作用(更改
data.crew
数组的顺序,这可能是不希望的)。 But it's kind of a "cute" solution so I figured I'd toss it out there. 但这是一种“可爱”的解决方案,所以我想我会把它扔出去。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.