获取JavaScript数组中的对象,其中appoints.id=calEvent.id

获取JavaScript数组中的对象,其中appoints.id=calEvent.id,javascript,jquery,arrays,jquery-week-calendar,Javascript,Jquery,Arrays,Jquery Week Calendar,如何在appoints数组中获取对象,其中appoints.id=calEvent.id appointments.push({ id: id, start: startFormatted, end: endFormatted, title: '<b>' + title + '</b><br><div id="event_body">' + body + '</div>', userId: userid, c

如何在
appoints
数组中获取对象,其中
appoints.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
}
appoints.push({
id:id,
开始:开始格式化,
结束:结束格式化,
标题:“+title+”
“+body+”, userId:userId, categoryId:categoryId, 柜台 }); eventRender:函数(calEvent,$event){ var id=calEvent.id; //获取Appoints数组中的对象,其中appoints.id=calEvent.id }
您只需在数组中循环查找所需的id:

for (var i = 0; i < appointments.length; i++) {
    if (appointments[i].id == calEvent.id) {
        // appointments[i] contains the desired id
        break;
    }
}
for(变量i=0;i
您需要在
约会
数组中循环查找匹配的
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;
        }
    }
}
eventRender:function(calEvent$event){
var id=calEvent.id;
对于(var i=0;i
那怎么办

appointments[id]={

    start: startFormatted,
    end: endFormatted, 
    title: '<b>' + title + '</b><br><div id="event_body">' + body + '</div>', 
    userId: userid,
    categoryId: categoryId,
    counterId: counter
};
糟糕的索引总是比挑剔的循环快&推送也很慢
编辑以添加以下内容:如果无法更改生成约会的方式,您仍然可以循环一次,以创建一个索引数组供eventrender函数使用,这将比每次调用eventrender时循环更有效。你的约会越多,索引的必要性就越明显

作为
calEvent
传递的对象是什么?它是jQuery weekcalendar插件中的日历事件。calEvent.id等于appointment.id.中的一个,如果您将其设置为-无用的comment@doonot;一次索引比每次循环快得多time@doonot另请参见编辑我的答案;为了进行不同的表述,您不需要将id属性设置为id的对象推送到id,而是将索引设置为id的元素添加到数组中(&这样就不需要id属性);不管怎样,如果你喜欢永远循环,我想这是你的决定
eventRender : function(calEvent, $event) {
  var id = calEvent.id;
  // get the object in the appointments-array where appointments.id = calEvent.id
  var obj=appointments[id]
 }