根据给定值检索json对象

iLa*_* ツ -1 javascript json

从下面的 json 中,如何根据给定的 applicationName 检索对象

{
    "apps": [
        {
            "applicationName": "myTestApp",
            "keys": [
                {
                    "key": "app-key",
                    "value": "ZDM0"
                },
                {
                    "key": "env-key",
                    "value": "YTE1Mm"
                }
            ]
        },
        {
            "applicationName": "hello",
            "keys": [
                {
                    "key": "app-key",
                    "value": "ZjIwZT"
                },
                {
                    "key": "env-key",
                    "value": "MDExMTc5N2"
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

因此,如果我的输入是 myTestApp,我想获取 myTestApp 对象

{
    "applicationName": "myTestApp",
    "keys": [
        {
            "key": "app-key",
            "value": "ZDM0"
        },
        {
            "key": "env-key",
            "value": "YTE1Mm"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Cer*_*rus 5

这应该可以解决问题:(
假设您data是存储数据的变量)

var result = data.apps.filter(function(app){
    return app.applicationName === "myTestApp";
});

if(result.length){         // If there are any results, then
    console.log(result[0]) // `result[0]` is the app with the right name.
}
Run Code Online (Sandbox Code Playgroud)