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

我有一个功能。这里我传递一个数组 applicationTabId = {"twitter": "15", "dropbox": "14"}; "}; 我有一个 ajax 成功函数。在这个变量 appName name 保存键名称为 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){


                        }

                   }        

                }

            },


        });

    });

 }


沧海一幻觉
浏览 81回答 1
1回答

守着星空守着你

应用如下循环:$.each(applicationTabId, function(key,value){&nbsp; &nbsp;console.log(key);&nbsp; &nbsp;console.log(value);});根据您现在提供的代码,更改您的代码,如下所示:function getapplicationlogouturl(applicationTabId) {&nbsp; &nbsp; FetchLogoutUrl =&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "applicationName": applicationTabId,&nbsp; &nbsp; };&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; url: obj.endpoint,&nbsp; &nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; &nbsp; data: JSON.stringify(FetchLogoutUrl),&nbsp; &nbsp; &nbsp; &nbsp; context: document.body,&nbsp; &nbsp; &nbsp; &nbsp; success: function (response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response != null ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < response.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var appName = response[i].name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var tabid = applicationTabId[appName];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //print key value data in console to check working fine&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(appName+'---'+tabid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; });}注意:- var appName每次迭代发生后,var tabid值都会改变。
打开App,查看更多内容
随时随地看视频慕课网APP