0

How can I get the object in the appointments array, where appointments.id = calEvent.id?

appointments.push({
  id: id,
  start: startFormatted,
  end: endFormatted, 
  title: '<b>' + title + '</b><br><div id="event_body">' + body + '</div>', 
  userId: userid,
  categoryId: categoryId,
  counterId: counter
});

eventRender : function(calEvent, $event) {
  var id = calEvent.id;

  // get the object in the appointments-array where appointments.id = calEvent.id
}
nimrod
  • 5,595
  • 29
  • 85
  • 149

3 Answers3

1

You just have to loop through the array looking for the desired id:

for (var i = 0; i < appointments.length; i++) {
    if (appointments[i].id == calEvent.id) {
        // appointments[i] contains the desired id
        break;
    }
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

You need to loop through your appointments array an find the matching id. Try this:

eventRender : function(calEvent, $event) {
    var id = calEvent.id;        
    for (var i = 0; i < appointments.length; i++) {
        if (appointments[i].id == id) {
            // do something with the appointment...
            break;
        }
    }
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

what about

appointments[id]={

    start: startFormatted,
    end: endFormatted, 
    title: '<b>' + title + '</b><br><div id="event_body">' + body + '</div>', 
    userId: userid,
    categoryId: categoryId,
    counterId: counter
};

then

eventRender : function(calEvent, $event) {
  var id = calEvent.id;
  // get the object in the appointments-array where appointments.id = calEvent.id
  var obj=appointments[id]
 }

poor index are always faster than fastidious looping & push is also slow edit to add that if you can not change the way you build your appointments you can still loop through it one time to create an indexed array to be use by your eventrender function which will be more efficient than looping every single time eventrender is called. The biggest your appointments, the most the necessity to index obviously

mikakun
  • 2,203
  • 2
  • 16
  • 24
  • it is if you set it as such - useless comment @doonot ; indexing once is way faster than looping every time – mikakun Feb 03 '13 at 15:41
  • @doonot also see edit to my answer; to formulate differently, instead of pushing an object with id property set to id, you add an element to your array with the index set to id (& that then don't even need an id property); anyway if you prefer to loop forever that's your call i guess – mikakun Feb 03 '13 at 15:45