0

I want to create an array from a loop which gets data from DB using AJAX. This is an example of the data:

events : [

                    {
                        id : 999,
                        title : 'Anglais Mr Azhar 2A',
                        start : '2015-12-08T14:30:00',
                        end : '2015-12-07T16:30:00'
                    },

                    {
                        id : 999,
                        title : 'ERP 1B',
                        start : '2015-12-08T16:45:00',
                        end : '2015-12-08T18:15:00'
                    },

                    {
                        id : 999,
                        title : 'PMC Mr Belasla 3A',
                        start : '2015-12-07T08:30:00',
                        end : '2015-12-07T10:00:00'
                    },
]

I want to assign the array created in the loop to events not like in this example where it is done manually.

So how can I create an Array from this data--id, title, start, ...-- to set events? Something like this:

events=myArray
6
  • Sorry, but what is boucle? Commented Feb 24, 2016 at 23:29
  • Could you try to explain your issue a bit better? I'm having trouble understanding the question as written. Commented Feb 24, 2016 at 23:30
  • @WalkerRandolphSmith i am sorry i meaned a loop Commented Feb 24, 2016 at 23:31
  • @Redmega I mean how can i create an array with id,title.... Commented Feb 24, 2016 at 23:32
  • looks like you are using Full Calendar and want to make a list of dynamic events that can be rendered into the calender. Look at the docs which cover how to import events (fullcalendar.io/docs) Commented Feb 24, 2016 at 23:36

1 Answer 1

1

Let says if you got already data that coming from ajax success, you can populate it like following :

// suppose you have this callback
// on ajax call
success : function ( data ) {
   // create empty array container
   var events = [];
   // loop over data
   // this depend on how the data variable
   // structure
   for ( var e in data ) {
      obj = {
         id : e.id,
         title : e.title,
         start : e.start,
         end : e.end
      }
      // add obj above into array container
      events.push( obj );
   }

}
Sign up to request clarification or add additional context in comments.

4 Comments

if data is coming as an array of objects already you can just do var events = data
@Redmega Yes and not, if the data coming from ajax holding another properties other than 4 stated above, it would be wrong.
I'm assuming he's making the ajax call to a rest service specifically meant to return the Event objects for a particular calendar. If this is the case then the assumption that var events = data; is a safe one to make. If it's not, then he probably needs to ponder over the design of his web service.
@Redmega Yes, should be like that. Seems like OP did't give enough explanation though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.