【问题标题】:fetch the associative array particular value with its key name获取关联数组的特定值及其键名
【发布时间】:2020-03-02 04:54:35
【问题描述】:

我有一个功能。这里我传递一个数组 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){

                        }
                   }        
                }
            },

        });
    });
 }

【问题讨论】:

  • {"twitter": "15", "dropbox": "14"} 是对象。使用var tabid = applicationTabId[appName];
  • 我在问题中修改了我的实际要求

标签: javascript php arrays


【解决方案1】:

如下应用循环:

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

根据您现在提供的代码,更改您的代码如下:

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);
                }
            }
        },
    });
}

注意:- var appNamevar tabid 的值将在每次迭代后更改。

【讨论】:

  • 感谢您的支持。这是带有json表示的pblm。我将键值设为“twitter”“11”,在 appName applicationTabId[appName] 中有一个额外的引号;谢谢你
  • 其实我也有同样的问题,感谢@AnantSingh---AlivetoDie 的回答;)
  • @DeepKakkar 很高兴为您提供帮助 :) :)
  • @user2830078 很高兴为您提供帮助:):)。快乐编码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
  • 2021-03-16
  • 2015-03-19
  • 2018-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
相关资源
最近更新 更多