我有这个对象:
var eventsDates = new Array(
"basket":"11-10-2012",
"soccer":"21-10-2012",
"soccer":"28-10-2012",
"baseball":"25-11-2012",
"baseball":"15-12-2012"
);
我需要通过比赛提取日期,例如只有足球而不是保存结果
var eventsDates = new Array(
"21-10-2012",
"28-10-2012"
);
任何建议
卢卡
答案 0 :(得分:1)
看起来你建议在阵列中有一系列对象:
var events = [
{"basket":"11-10-2012"},
{"soccer":"21-10-2012"},
{"soccer":"28-10-2012"},
{"baseball":"25-11-2012"},
{"baseball":"15-12-2012"}
];
var count = events.length, dates = [], m;
while (count--)
if (m = events[count]["soccer"])
dates.push(m);
console.log(dates);
答案 1 :(得分:1)
请勿混用arrays
和objects
使用其中一个:
var eventDates = [
"11-10-2012",
"21-10-2012",
"28-10-2012",
"25-11-2012",
"15-12-2012"
];
OR
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"}
];
答案 2 :(得分:0)
我会为这种任务考虑不同的数据结构:
var eventDates = {
Soccer: [{
date: ...
]},
Basket: [{
date: ...
]}
};
然后你可以轻松访问f.e.所有足球日期:
for (var i = 0; i <= eventDates.Soccer.length; i++) {
// Do something with eventDates.Soccer[i]
}
答案 3 :(得分:0)
var eventsDates = {
"basket":["11-10-2012"],
"soccer":["21-10-2012","28-10-2012"],
"baseball":["25-11-2012","15-12-2012"]
}
var soccerDates = eventsDates.soccer;