简体   繁体   English

Angular 6:如何使用Angular 6从Json数组获取特定数据

[英]Angular 6 : how to get specific data from Json array using Angular 6

I have retrieved data from the JSON array in Angular 6 using GET method. 我已经使用GET方法从Angular 6中的JSON数组中检索了数据。 I have this JSON data as an example and I want to find employee "TEST4KRWN" in which team(s) is located. 我以这个JSON数据为例,我想找到团队所在的员工“ TEST4KRWN”。

How can I write a JSON-query using angular 6? 如何使用angular 6编写JSON查询?

My JSON looks like this: 我的JSON看起来像这样:

{
    "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: 您可以使用find()在一行中完成此操作:

 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 : myjson为您的json数据:

 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" TEAM_NAME_list您将获得所有拥有员工“ TEST4KRWN”的团队

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM