简体   繁体   中英

How to create a customized javaScript array?

I have a little web app. And I use a calender(code from here FullCalendar ) there inside a .jsp file. I use Spring mvc architecture. So when this page loaded a controller which is responsible for this page loading will add an attribute called calendarEventList to the model . 'calendarEventList' is an ArrayList<calObject> . calObject is a class which contain the details about the even.

I can access those details in the jsp by ${calEvent.eventID} where calEvent is an Object from that ArrayList.

In FullCalendar the event load can be called like below

$('#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
    }
]
});

I want is like below

$('#calendar').fullCalendar({
   events: //read the `calendarEventList` and create a suitable array 
});

I have title , start , end .. details in a Oblect of the calendarEventList . So I want to create the list which I need to give to the fullcalendar . How can I create such a kind of array in JS. It's structure should match [{},{},{},...,{},{}] as given in the example. {} includes a detail about one Object in that calendarEventList ArrayList.

any detailed description will be highly appreciated.

If i truly understand your question ( you have array of objects, all objects contains different field, but each object contains title, start (and end)). So your task is filter this array.

Solution:

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}]);

Output will be filtered array of objects with start, title, (end) fields;

Try demo

Let try with my example:

 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']}); 

on events: employees put like this,, Hope it help full. thanks you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM