如何访问此 javascript MAP 对象中的数据?

发布于 2024-10-07 19:16:15 字数 1505 浏览 2 评论 0原文

我正在进行一个返回 XML 数据的 AJAX 调用,这是我的“success:”函数(回调):

success: function (data) { 
        var $rowArray = $(data).find("[nodeName=z:row]");
          $rowArray.each(function(index) { // for each date put it into calMap.
          calMap[$(this)[index].title] = $(this).attr("ows_Title");
          calMap[$(this)[index].date]  = $(this).attr("ows_EventDate");
          });
         }   

calMap 是在函数外部声明的全局 javascript 对象。

var calMap = {};

我想要做的是创建一个可以传入 title 的函数,让它在 calMap 中搜索该标题,如果找到,则返回特定对象,然后我将能够访问该对象的日期属性。

问题是,我似乎无法访问插入到 calMap 对象中的数据。首先,我只想打印地图。尝试eval'ing它,尝试提醒calMap[0],尝试提醒calMap[0].title,但什么也没有。有人可以帮我解决这个问题吗?谢谢!

更新: 我想做这样的事情:

var data = getData("myTitle");

function getData(title) {
// if title is in calMap, something like this?
var result = (calMap[title])); 
return result;  // returns an object or NOTHING
}

然后我将检查日期是否已定义,如果是,我将访问其属性(即data.date)。这有意义吗

? : 我最终使用了一个数组。仍然认为我应该能够使用对象 MAP,但需要完成我的项目。

这是访问数组项的代码的最终代码:

function hasCalDate(code)
{
    var matched = "";
    for (var f=0;f<calMap.length;f++){
        var re = new RegExp(code);
        if (re.test(calMap[f].title))
        {
        matched = calMap[f].title+','+calMap[f].date;
        }
    }
return matched;
};

谢谢大家。

I'm making an AJAX call which returns XML data, and this is my 'success:' function (callback):

success: function (data) { 
        var $rowArray = $(data).find("[nodeName=z:row]");
          $rowArray.each(function(index) { // for each date put it into calMap.
          calMap[$(this)[index].title] = $(this).attr("ows_Title");
          calMap[$(this)[index].date]  = $(this).attr("ows_EventDate");
          });
         }   

calMap is a global javascript object declared outside of the function.

var calMap = {};

What I want to do is create a function where I can pass in a title, have it search calMap for that title, and if found, the specific object is returned and I'll be able to access the date property for that object.

Problem is, I can't seem to access the data I insert into the calMap object. For starters, I just want to print the map. Tried eval'ing it, tried alerting calMap[0], tried alerting calMap[0].title, but nothing. Can someone help me with this? Thanks!

Update:
I want to do something like this:

var data = getData("myTitle");

function getData(title) {
// if title is in calMap, something like this?
var result = (calMap[title])); 
return result;  // returns an object or NOTHING
}

then i'll check if date is defined or not, and if it is, i'll access its properties (ie. data.date. That make sense?

ANSWER:
I ended up using an array. STILL think I should be able to use the object MAP, but needed to get my project done.

Here's the final code for the code that accesses the array items:

function hasCalDate(code)
{
    var matched = "";
    for (var f=0;f<calMap.length;f++){
        var re = new RegExp(code);
        if (re.test(calMap[f].title))
        {
        matched = calMap[f].title+','+calMap[f].date;
        }
    }
return matched;
};

Thanks everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱绻入梦 2024-10-14 19:16:15

您需要将calMap初始化为数组(即方括号,而不是大括号):

var calMap = [];

然后,在您的each函数中,我猜你想要更像的东西

calMap.push({
    title: $(this).attr("ows_Title"),
    date: $(this).attr("ows_EventDate")
});

You need to initialize calMap as an array (i.e. square brackets, not curly ones):

var calMap = [];

Then, inside your each function, I'm guessing you want something more like

calMap.push({
    title: $(this).attr("ows_Title"),
    date: $(this).attr("ows_EventDate")
});
离笑几人歌 2024-10-14 19:16:15

您的问题是 success 函数仅在 AJAX 请求完成时运行。如果您想安全地访问 calMap,您需要在回调内部执行此操作。

Your problem is that the success function is only run when your AJAX request completes. If you want to access calMap safely, you need to do so inside your callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文