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

时间:2019-05-15 10:13:08

标签: json angular

我已经使用GET方法从Angular 6的JSON数组中检索了数据。 我以这个JSON数据为例,我想找到团队所在的员工“ TEST4KRWN”。

如何使用angular 6编写JSON查询?

我的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"
        }

    ]
}

3 个答案:

答案 0 :(得分:1)

您可以使用find()一行来做到这一点:

 const selectedEmployee = data.teams.find(
   t => t.EMPLOYEES.find(e => e === 'TEST4KRWN')
 );

答案 1 :(得分:0)

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

现在您可以从中了解团队,您可以访问任何对象。

答案 2 :(得分:0)

myjson是您的json数据:

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

TEAM_NAME_list中,您将获得所有拥有员工“ TEST4KRWN”的团队