简体   繁体   English

根据嵌套数组中的匹配键值获取对象

[英]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

jsfiddle here jsfiddle在这里

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.

相关问题 通过基于 javascript 中的匹配键添加键值来创建数组 object - create array object by add key value based on matching key in javascript 根据对象中的其他键获取数组中对象的键值 - Get value of key of object in array based on other key in object 根据键值对条件动态将 object 推入嵌套数组 - dynamically push object into nested array based on key,value pair condition 在嵌套对象中匹配键值对的路径 - Path to matching key value pair in a nested object Javascript:从 object 的嵌套数组中获取值并使其成为 object 键 - Javascript: get value from nested array of object and make it an object key 根据键过滤object的数组,只得到键值 - filter an array of object based on a key and get only key value 从 object 获取键值并将其添加到另一个匹配嵌套值的 object - get key value from object and add it to another object matching nested value 将 2 个 arrays 转换为键值 object,一个数组保存键,第二个是匹配索引值的嵌套数组 - convert 2 arrays into key value object, one array hold the keys, they second is nested array of matching indexed values javascript根据键值过滤嵌套对象 - javascript filter nested object based on key value 根据匹配关键字Javascript更新对象数组中的同一对象 - Update the same object in an array of object based on Matching key Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM