简体   繁体   English

在JSON对象中查找特定值

[英]Find specific value in JSON object

{
  "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. 这是tmdb php api的tmdb示例输出。 How to get Directors name with jquery for example? 例如,如何通过jquery获取Directors名称? The order of crew output is random. 机组输出的顺序是随机的。

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. 无论您使用的是jQuery还是纯JavaScript或任何其他具有数组的语言,这都是相同的。

In your case, you want to search for an object with the property job equal to the string "Director" . 在您的情况下,您要搜索属性job等于字符串"Director"

Once you find it, you want to return the property name from that object. 找到它后,您想从该对象返回属性name

You could do this with a for loop: 您可以使用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;
    }
  }
}

Or perhaps a while loop: 或者也许是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++;
  }
}

Using the built-in method Array.prototype.find and arrow functions, you can simplify the code to: 使用内置方法Array.prototype.find和arrow函数,可以将代码简化为:

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

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

I like using some() . 我喜欢使用some()

aMovie.crew.some(function(member) {

    var job = member.job.toLowerCase();

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM