0

I have retrieved data from the JSON array in Angular 6 using GET method. I have this JSON data as an example and I want to find employee "TEST4KRWN" in which team(s) is located.

How can I write a JSON-query using angular 6?

My JSON looks like this:

{
    "teams": [
          {
            "EMPLOYEES": [
                "TEST4KRWN",
                "TESTV6A3M"
            ],
            "TEAM_LEAD": "TEST3L671",
            "TEAM_ID": 7,
            "TEAM_NAME": "Quantum Computing"
        },
        {
            "EMPLOYEES": [
                "TESTEK9H4",
                "TEST4KRWN",
                "TESTWIOTA"
            ],
            "TEAM_LEAD": "TESTB2SEA",
            "TEAM_ID": 8,
            "TEAM_NAME": "Language"
        },

        {
            "EMPLOYEES": [
                "TESTWJJ7W"
            ],
            "TEAM_LEAD": "TESTOOARS",
            "TEAM_ID": 10,
            "TEAM_NAME": "General Services"
        },
        {
            "EMPLOYEES": [
                "TEST1S09V",
                "TEST4KRWN"
            ],
            "TEAM_LEAD": "TESTYUK99",
            "TEAM_ID": 11,
            "TEAM_NAME": "Logistik"
        },
        {
            "EMPLOYEES": [
                "TEST7QTWJ",
                "TEST4KRWN",
                "TESTMKT1K"
            ],
            "TEAM_LEAD": "TESTVYTHP",
            "TEAM_ID": 12,
            "TEAM_NAME": "R&D"
        }

    ]
}

3 Answers 3

2

You can use find() to do that in one line:

 const selectedEmployee = data.teams.find(
   t => t.EMPLOYEES.find(e => e === 'TEST4KRWN')
 );
Sign up to request clarification or add additional context in comments.

Comments

0
const selectedTeam = null;
teams.forEach((team) => {
    selectedTeam = team.EMPLOYEES.filter(function (employee, pos) {
        return employee === "TEST4KRWN";
    });
});

Now you could know the team from that you can access any object.

1 Comment

You can use some in place of forEach and return selectedTeam. In this way you avoid keep iterating if you have already found the selected team
0

Let myjson be the json data of yours :

 let TEAM_NAME_list =[]
    for (let item of myjson.teams){
    if(item.EMPLOYEES.includes('TEST4KRWN')){
    TEAM_NAME_list.push(item.TEAM_NAME)}
    }

in TEAM_NAME_list you will get all team(s) who have employee "TEST4KRWN"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.