0

如何获取appointments数组中的对象,在哪里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
}
4

3 回答 3

1

您只需遍历数组以查找所需的 id:

for (var i = 0; i < appointments.length; i++) {
    if (appointments[i].id == calEvent.id) {
        // appointments[i] contains the desired id
        break;
    }
}
于 2013-02-03T15:34:27.607 回答
1

您需要遍历appointments数组并找到匹配的id. 试试这个:

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;
        }
    }
}
于 2013-02-03T15:34:38.570 回答
0

关于什么

appointments[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
  var obj=appointments[id]
 }

糟糕的索引总是比挑剔的循环更快,推送也很慢比每次调用 eventrender 时都循环更有效。最大的约会,最需要索引的明显

于 2013-02-03T15:35:06.187 回答