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"
    }
}

Hi! This is a tmdb example output of tmdb php api. How to get Directors name with jquery for example? The order of crew output is random.

nem035
  • 34,790
  • 6
  • 87
  • 99
michel
  • 11
  • 1
  • 4
  • Possible duplicate of [Find object by id in an array of JavaScript objects](http://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – JJJ Feb 26 '17 at 19:17
  • ```var object = [your object]; var directors = []; directors = jQuery.grep(object.crew, function( element, i ) { return element.name === 'Director'; });``` if there are several directors in your crew? – jedgard Feb 26 '17 at 19:23

3 Answers3

0

Generally when you are searching for something in an array, you want to iterate through the array until you find it. This is the same whether you use something like jQuery or plain JavaScript or any other language that has arrays.

In your case, you want to search for an object with the property job equal to the string "Director".

Once you find it, you want to return the property name from that object.

You could do this with a for loop:

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

Or perhaps a while loop:

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

Using the built-in method Array.prototype.find and arrow functions, you can simplify the code to:

function findDirectorName(data) {
  let director = data.crew.find(crewMember => crewMember.job === 'Director');
  return director ? director.name : undefined;
}
nem035
  • 34,790
  • 6
  • 87
  • 99
0

Try this:

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));
dev8080
  • 3,950
  • 1
  • 12
  • 18
0

I like using some().

aMovie.crew.some(function(member) {

    var job = member.job.toLowerCase();

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

}
Darkrum
  • 1,325
  • 1
  • 10
  • 27