简体   繁体   English

获取关联数组特定值及其键名

[英]fetch the associative array particular value with its key name

i have a function .我有一个功能。 here i am passing an array applicationTabId = {"twitter": "15", "dropbox": "14"};在这里我传递一个数组 applicationTabId = {"twitter": "15", "dropbox": "14"}; "}; I have an ajax success function .In this the variable appName name holds the key names as twitter , dropbox etc. I got this from database as ajax response. i want to find the value associated with this from the array. variable appName holding the key names and iam getting it from database as ajax response. i need to check the corresponding value of this name from array. "}; 我有一个 ajax 成功函数。在这个变量 appName 名称中保存关键名称为 twitter、dropbox 等。我从数据库中作为 ajax 响应得到它。我想从数组中找到与此相关的值。变量 appName保存键名并从数据库中获取它作为 ajax 响应。我需要从数组中检查此名称的相应值。

function getapplicationlogouturl(applicationTabId) {


     chrome.storage.sync.get("endpoint", function (obj) {
        FetchLogoutUrl =  {
            "applicationName": applicationTabId,
            "sdcode": sdcode,
            "type": "Chrome Fetch Application Logout Url"
        };
        $.ajax({
            type:    "POST",
            url:      obj.endpoint,
            dataType: "json",
            data:    JSON.stringify(FetchLogoutUrl),
            context: document.body,
            timeout: globalTimeout,
                success: function (response) {

                if (response != null ) {

                        for (var i = 0; i < response.length; i++) {
                        var appName = response[i].name;
                        console.log(applicationTabId);
// o/p as {"twitter": "15", "dropbox": "14"}
                        console.log(appName);
//o/p as twitter
//dropbox

                      var tabid = applicationTabId.appName;
                      //var tabid = applicationTabId[appName];
                      console.log(tabid);
   //o/p as undefined
                      console.log(appName+'---'+tabid);


                        if (appName in applicationTabId){

                        }
                   }        
                }
            },

        });
    });
 }

Apply loop like below:应用循环如下:

$.each(applicationTabId, function(key,value){
   console.log(key);
   console.log(value);
});

Based on code you provided now, change your code like below:根据您现在提供的代码,更改您的代码,如下所示:

function getapplicationlogouturl(applicationTabId) {
    FetchLogoutUrl =  {
        "applicationName": applicationTabId,
    };
    $.ajax({
        type: "POST",
        url: obj.endpoint,
        dataType: "json",
        data: JSON.stringify(FetchLogoutUrl),
        context: document.body,
        success: function (response) {
            if (response != null ) {
                for (var i = 0; i < response.length; i++) {
                    var appName = response[i].name;
                    var tabid = applicationTabId[appName];
                    //print key value data in console to check working fine
                    console.log(appName+'---'+tabid);
                }
            }
        },
    });
}

Note:- var appName and var tabid value will be changed after each iteration happen.注意:- var appNamevar tabid值将在每次迭代发生后更改。

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

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