[英]issue with undefined var in javascript, jquery. how to?
I am using Date.js to display some dates and a weird thing happens. 我正在使用Date.js来显示一些日期,并且发生了一件奇怪的事情。 If I use it in an array formed from within an each
statement. 如果我在each
语句中形成的数组中使用它。
Here is what i mean: 这是我的意思:
If I pass and use the events_array
to the test
function array everything works fine, but if I use the eventsArray
one, that should look the same, I get: 如果我将events_array
传递并使用到test
函数数组,则一切正常,但是如果我使用eventsArray
,则外观应该相同,我得到:
startDate is undefined
...new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()).getTime()...
function getTest() {
$.ajax({
type: "GET",
url: "/index",
dataType: "json",
success: function (data) {
var eventsArray = new Array();
jQuery.each(data, function (i, val) {
eventsArray.push(
new Array({
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10),
}));
});
var events_array = new Array({
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10),
}, {
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10),
});
test(events_array);
}
});
}
Any ideas? 有任何想法吗?
Thanks 谢谢
Within your test function try following: 在测试功能中,请尝试以下操作:
function test(eventsArray) {
// try this
eventsArray[0][0].startDate
}
eventsArray
looks like following: eventsArray
如下所示:
eventsArray = [
[
{
startDate: ..
endDate: ..
}
]
];
But you should make it easier: 但是您应该使它更容易:
var eventsArray = new Array();
eventsArray.push({
startDat: ..
endDate: ..
});
Don't use additional array within eventsArray
if you not need it. 如果不需要,请不要在eventsArray
使用其他数组。
and to access that values use: 要访问该值,请使用:
eventsArray[0].startDate;
eventsArra[0].endDate;
events_array
, you'll see that, you're not using additional array when you push data within it. 如果查看events_array
,您会看到,在其中推送数据时,您没有使用其他数组。 You probably meant to fill eventArray
with objects, not arrays with a single object in each one: 您可能打算用对象填充eventArray
,而不是用每个对象中的单个对象填充数组:
eventsArray.push({ // NOT new Array...
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10)
}); });
your two arrays are not identicals : var eventsArray = new Array(); 您的两个数组不相同:var eventsArray = new Array();
jQuery.each(data, function (i, val) {
eventsArray.push(
new Array({
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10),
}));
});
/* here eventsArray is like that [[{
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10),
}]] Look at the two [[ or ]] : array of object in an array
*/
var events_array = new Array({
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10),
}, {
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10),
});
/* here events_array is like that [{
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10),
}, {}] only one [ ... array of objects ...
*/
i think you should use : 我认为你应该使用:
eventsArray.push({
startDate: new Date(2011, 07, 20, 15, 50),
endDate: new Date(2012, 00, 10)
}); });
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.