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"
}
]
}
You can use find() to do that in one line:
const selectedEmployee = data.teams.find(
t => t.EMPLOYEES.find(e => e === 'TEST4KRWN')
);
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.
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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.