我有一个小网络应用程序。我在 .jsp 文件内使用日历(代码来自 here FullCalendar )。
我使用Spring mvc架构。因此,当此页面加载时,负责此页面加载的 Controller 将添加一个名为 calendarEventList
的属性。到model
。
“calendarEventList”是 ArrayList<calObject>
。 calObject 是一个包含事件详细信息的类。
我可以通过 ${calEvent.eventID}
访问 jsp 中的这些详细信息哪里calEvent
是该 ArrayList 中的一个对象。
在 FullCalendar 中,可以像下面这样调用事件加载
$('#calendar').fullCalendar({
events: [
{
title : 'event1',
start : '2010-01-01'
},
{
title : 'event2',
start : '2010-01-05',
end : '2010-01-07'
},
{
title : 'event3',
start : '2010-01-09 12:30:00',
allDay : false // will make the time show
}
]
});
我想要的是如下
$('#calendar').fullCalendar({
events: //read the `calendarEventList` and create a suitable array
});
我有title
, start
, end
..详细信息在 Oblect of the calendarEventList
。
所以我想创建一个列表,我需要将其提供给 fullcalendar
。我怎样才能在JS中创建这样一种数组。
它的结构应该匹配 [{},{},{},...,{},{}]
如示例中所示。 {}
包含有关一个对象的详细信息,其中 calendarEventList
数组列表。
任何详细的描述都将受到高度赞赏。
最佳答案
如果我真正理解你的问题(你有对象数组,所有对象都包含不同的字段,但每个对象都包含标题、开始(和结束))。所以你的任务是过滤这个数组。
解决方案:
function factory(array){
return array.map(function(item){
var instance = {title: item.title, start: item.start};
if(item.end) instance.end = item.end;
return item;
});
}
var res = factory([{ item: 1, title: 2, start: 4, an: 123, pp: "asdf"}, { item: 2, title: 2, start: 4, end: 5}, { item: 3, title: 2, start: 4}]);
输出将是经过过滤的对象数组,其中包含开始、标题、(结束)字段;
尝试demo
关于javascript - 如何创建自定义的 JavaScript 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21928996/