[英]How to create customized object from another object in javascript
[英]How to create a customized javaScript array?
我有一个小的网络应用程序。 我在.jsp文件中使用了压延机(来自这里的代码FullCalendar )。 我使用Spring MVC架构。 所以,当这个页面加载控制器,负责此页面加载会添加称为属性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
});
我Oblect of the calendarEventList
的Oblect of the calendarEventList
具有title
, start
, end
..详细信息。 所以我想创建一个列表,我需要提供给全fullcalendar
。 如何在JS中创建这种数组。 如[{},{},{},...,{},{}]
其结构应与[{},{},{},...,{},{}]
匹配。 {}
包含有关该calendarEventList
ArrayList中的一个对象的详细信息。
任何详细的描述将是高度赞赏的。
如果我真正理解您的问题(您有对象数组,则所有对象都包含不同的字段,但是每个对象都包含标题,开始(和结束))。 因此,您的任务是过滤该数组。
解:
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}]);
输出将被过滤后的对象数组,其中包含开始,标题,(结束)字段;
试试演示
让我们尝试以下示例:
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var employees = [];
employees.push({ id: 111, title: "Testing Event 001", start: new Date(y, m, d-5,10,30) , end: new Date(y, m, d-5,12,30),className: ['yellow-event', 'black-text-event']});
employees.push({ id: 111, title: "Testing Event 002", start: new Date(y, m, d-3,10,30) , end: new Date(y, m, d-3,12,30),className: ['yellow-event', 'black-text-event']});
关于events: employees
们这样说,希望它能对大家有所帮助。 谢谢..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.