Find specific JSON key and return its value in Javascript(查找特定的JSON键并在Javascript中返回其值)

I have some JSON from an API which I am logging in the console.
This is easy for something like the id where I can do this…

let movieID = out.id;
console.log(movieID)

But how can I also return the ‘name’ of the person whose job is specifically ‘Director’ from JSON example like this? Basically I need to do something like ‘if someone has the JOB of DIRECTOR, log their name to the console.

{
    "id": 37291,
    "credits": {
        "crew": [
            {
                "name": "John Smith",
                "job": "Producer"
            },
            {
                "name": "Mary Jones",
                "job": "Director"
            }
        ]
    }
}

Solution:

You can get the list of the crew, like you described above. Then find the first entry in that list where the job equals “Director” and put it in a variable:

let director = out.credits.crew.find(member => member.job == "Director");

You can then log the data, as you did for the movie itself.

————————

我在控制台中记录了一些来自API的JSON。
这对于像id这样的东西来说很容易,我可以在那里做这件事。。。

let movieID = out.id;
console.log(movieID)

但是,我如何也能从JSON示例中返回其工作具体为“Director”的人的“name”呢?基本上,我需要做一些类似“如果有人担任主管,请将其姓名记录到控制台”。

{
    "id": 37291,
    "credits": {
        "crew": [
            {
                "name": "John Smith",
                "job": "Producer"
            },
            {
                "name": "Mary Jones",
                "job": "Director"
            }
        ]
    }
}

解决方法:

你可以得到船员名单,就像你上面描述的那样。然后在该列表中找到作业等于“Director”的第一个条目,并将其放入变量中:

let director = out.credits.crew.find(member => member.job == "Director");

然后可以记录数据,就像对电影本身所做的那样。