不要混合arrays
使用objects
其中一种:
var eventDates = [
"11-10-2012",
"21-10-2012",
"28-10-2012",
"25-11-2012",
"15-12-2012"
];
或者
var eventDates = {
"basket":"11-10-2012",
"soccer":"21-10-2012",
"soccer":"28-10-2012",
"baseball":"25-11-2012",
"baseball":"15-12-2012"
};
也就是说,您不能重复使用相同的密钥(如baseball
),因此最好的结构将是以下之一:
var eventDates = {
"basket": ["11-10-2012"],
"soccer": ["21-10-2012", "28-10-2012"]
"baseball": ["25-11-2012", "15-12-2012"]
};
并像访问eventDates.baseball
$.each(eventDates.baseball, function (k, v) {
alert(v);
});
您也可以执行以下操作,但随后您将不得不使用自定义提取器,这将涉及在您只需要特定日期的任何时候迭代整个数组name
。
var eventDates = [
{"name": "basket", "date":"11-10-2012"},
{"name":"soccer", "date":"21-10-2012"},
{"name": "soccer", "date":"28-10-2012",
{"name": "baseball", "date":"25-11-2012"},
{"name":"baseball", "date":"15-12-2012"}
];