Roger Erik Tinch
Roger Erik Tinch

Reputation: 101

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.

And here's my javascript where I'm setting the year variable based on the value of 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.

Upvotes: 0

Views: 3115

Answers (2)

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

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/

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));

Upvotes: 3

broofa
broofa

Reputation: 38122

For a slightly "different" approach, sort by job first. E.g.

data.crew.sort(function(a,b) {
  return a.job == 'Director' ? -1 : 1;
});
console.log(data.crew[0].name); // => name of Director

jsfiddle here

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). But it's kind of a "cute" solution so I figured I'd toss it out there. :)

Upvotes: 0

Related Questions