Question

Hello currently I am trying to get a specific value, but not entirely sure how.

Below is part of the JSON data that i am using. the data is set to equal to "entries"

 var entries = "credits": {
    "crew": [
        {
            "id": 7469,
            "name": "JimUhls",
            "department": "Writing",
            "job": "Author",
            "profile_path": null
        },
        {
            "id": 7474,
            "name": "RossGraysonBell",
            "department": "Production",
            "job": "Producer",
            "profile_path": null
        },
        {
            "id": 7475,
            "name": "CeánChaffin",
            "department": "Production",
            "job": "Producer",
            "profile_path": null
        },
        {
            "id": 1254,
            "name": "ArtLinson",
            "department": "Production",
            "job": "Producer",
            "profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
        },
        {
            "id": 7477,
            "name": "JohnKing",
            "department": "Sound",
            "job": "OriginalMusicComposer",
            "profile_path": null
        },
        {
            "id": 7478,
            "name": "MichaelSimpson",
            "department": "Sound",
            "job": "OriginalMusicComposer",
            "profile_path": null
        },
        {
            "id": 7479,
            "name": "JeffCronenweth",
            "department": "Camera",
            "job": "DirectorofPhotography",
            "profile_path": null
        },
        {
            "id": 7480,
            "name": "JamesHaygood",
            "department": "Editing",
            "job": "Editor",
            "profile_path": null
        },
        {
            "id": 7481,
            "name": "LarayMayfield",
            "department": "Production",
            "job": "Casting",
            "profile_path": null
        },
        {
            "id": 1303,
            "name": "AlexMcDowell",
            "department": "Art",
            "job": "ProductionDesign",
            "profile_path": null
        },
        {
            "id": 7763,
            "name": "RenKlyce",
            "department": "Sound",
            "job": "SoundEditor",
            "profile_path": null
        },
        {
            "id": 7764,
            "name": "RichardHymns",
            "department": "Sound",
            "job": "SoundEditor",
            "profile_path": null
        },
        {
            "id": 7467,
            "name": "DavidFincher",
            "department": "Directing",
            "job": "Director",
            "profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
        },
        {
            "id": 7468,
            "name": "ChuckPalahniuk",
            "department": "Writing",
            "job": "Novel",
            "profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
        }
    ]
}
}

I then parse through the data using this:

crew_member = 0,
crew_members = [];

for (crew_member = 0; crew_member < entries.credits.crew.length; crew_member++) {
    crew_members.push(entries.credits.crew[crew_member].job + ': ' + entries.credits.crew[crew_member].name);
}

document.getElementById('Crew').innerHTML = crew_members.join(',');

Everything works on my end. The problem I am having is how to specifically get for example the director and the director only.

Was it helpful?

Solution

It's a simple if statement:

for (crew_member = 0; crew_member < entries.credits.crew.length; crew_member++) {
    if (entries.credits.crew[crew_member].job == "Director") {
        crew_members.push(entries.credits.crew[crew_member].job + ': ' + entries.credits.crew[crew_member].name);
    }
}

OTHER TIPS

try jquery

jQuery.grep(entries.credits.crew,function(n,i){ return (n.job == "Director") });

This should return you array of crew elements which has job as directors.

Thanks.

Simplest Way

entries.credits.crew.filter(function(a){ return a.job == 'Director'; });

You'll get a separate array of objects where job is "Director"

You can filter the items and still return and array like this:

entries.credits.crew.filter(function(member){ return member.job === "DirectorofPhotography"  } );

You can also add this:

 entries.credits.crew.filter(function(member){ return member.job === "DirectorofPhotography"  } ).map(function(member){
 return member.job+ ': ' + member.name;}).join(',');

Update 1 This is based on this Object, since the one provide does not seem to be in the correct format:

var entries = { "credits" : {"crew":[
            {
                "id": 7469,
                "name": "JimUhls",
                "department": "Writing",
                "job": "Author",
                "profile_path": null
            },
            {
                "id": 7474,
                "name": "RossGraysonBell",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 7475,
                "name": "CeánChaffin",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 1254,
                "name": "ArtLinson",
                "department": "Production",
                "job": "Producer",
                "profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
            },
            {
                "id": 7477,
                "name": "JohnKing",
                "department": "Sound",
                "job": "OriginalMusicComposer",
                "profile_path": null
            },
            {
                "id": 7478,
                "name": "MichaelSimpson",
                "department": "Sound",
                "job": "OriginalMusicComposer",
                "profile_path": null
            },
            {
                "id": 7479,
                "name": "JeffCronenweth",
                "department": "Camera",
                "job": "DirectorofPhotography",
                "profile_path": null
            },
            {
                "id": 7480,
                "name": "JamesHaygood",
                "department": "Editing",
                "job": "Editor",
                "profile_path": null
            },
            {
                "id": 7481,
                "name": "LarayMayfield",
                "department": "Production",
                "job": "Casting",
                "profile_path": null
            },
            {
                "id": 1303,
                "name": "AlexMcDowell",
                "department": "Art",
                "job": "ProductionDesign",
                "profile_path": null
            },
            {
                "id": 7763,
                "name": "RenKlyce",
                "department": "Sound",
                "job": "SoundEditor",
                "profile_path": null
            },
            {
                "id": 7764,
                "name": "RichardHymns",
                "department": "Sound",
                "job": "SoundEditor",
                "profile_path": null
            },
            {
                "id": 7467,
                "name": "DavidFincher",
                "department": "Directing",
                "job": "Director",
                "profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
            },
            {
                "id": 7468,
                "name": "ChuckPalahniuk",
                "department": "Writing",
                "job": "Novel",
                "profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
            }
        ]
    } };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top