如何访问此 javascript MAP 对象中的数据?
我正在进行一个返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
calMap
初始化为数组(即方括号,而不是大括号):然后,在您的
each
函数中,我猜你想要更像的东西You need to initialize
calMap
as an array (i.e. square brackets, not curly ones):Then, inside your
each
function, I'm guessing you want something more like您的问题是
success
函数仅在 AJAX 请求完成时运行。如果您想安全地访问calMap
,您需要在回调内部执行此操作。Your problem is that the
success
function is only run when your AJAX request completes. If you want to accesscalMap
safely, you need to do so inside your callback.