简体   繁体   English

如何循环 jquery 中的第二个元素?

[英]How can I loop the second element in jquery?

this is my object of events这是我的事件对象

let aEvents = [{
  id: '1',
  location: 'asd',
  dates: '2019-12-21 16:00:00-2019-12-21 17:30:00',
  position: '3',
  status: true,
  sessions: [{
    id: '1',
    text: 'Random text',
    startEndDate: '2019-12-21 16:00:00 - 2019-12-21 17:30:00'
  }, {
    id: '2',
    text: 'Random text 2',
    startEndDate: '2017-12-21 16:00:00 - 2019-12-21 17:30:00'
  }]
}];

I have this function where i recive my aEvents.我有这个功能,我可以在那里接收我的 aEvents。 With the append function im changing some html content.使用 append 函数更改一些 html 内容。 Im also printing the results in a table inside a foreach so it creates a new table for each event.我还在 foreach 内的表中打印结果,以便为每个事件创建一个新表。 The problem come ones I want to acces the sessions.问题来了,我想访问会话。 How could I do that?我怎么能那样做?

 addEvents = function () {
      let events = this.myEvents;
      let checkedEvents = [];
      let that = this;

      var content = this.$('#eventsBox');
      content.find('event-container').remove();

      this.$('#check-list input:checked').each(function() {
        checkedEvents.push(that.$(this).data('index'));
        let index = that.$(this).data('index');

        content.find('#events-content').append(`
          <div class="event-container">
            <div class="row">
              <div class="col-12">
                <table class="table" id="eventT">
                  <thead>
                    <tr>
                      <th scope="col">ID</th>
                    <tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td>
                        ${events[index].id}
                        // How can i acces the sessions[index] to print all of them
                        ${events[index].sessions[index].id}
                      </td>
               ...

You need to add one more loop which will loop over sessions.您需要再添加一个循环来循环会话。 Currently you are using the external index for sessions which is incorrect.目前您正在使用不正确的会话的外部索引。

You should also iterate the sessions object, but with another index, because the index variable is for the events object.您还应该迭代会话对象,但使用另一个索引,因为索引变量用于事件对象。 Also, you can extract the code and insert it in the place of the id:此外,您可以提取代码并将其插入到 id 的位置:

 addEvents = function () { let events = this.myEvents; let checkedEvents = []; let that = this; var content = this.$('#eventsBox'); content.find('event-container').remove(); let idFromSessions = {}; for (let x = 0; x < events.length; x++) { idFromSessions[x] = ''; for (let y = 0; y < events[x].sessions.length; y++) { idFromSessions[x] += events[x].sessions[y].id; } } this.$('#check-list input:checked').each(function() { checkedEvents.push(that.$(this).data('index')); let index = that.$(this).data('index'); content.find('#events-content').append(` <div class="event-container"> <div class="row"> <div class="col-12"> <table class="table" id="eventT"> <thead> <tr> <th scope="col">ID</th> <tr> </thead> <tbody> <tr> <td> ${events[index].id} // Using this new variable, you can print all of them ${idFromSessions[index]} </td>

A Javascript solution to this对此的 Javascript 解决方案

for(var i=0;i<aEvents.length;i++) {
 var sessions = aEvents[i].sessions
 if(sessions !== undefined && sessions.length >0) {
   for(var j=0;j<sessions.length;j++) {
    //you are now looping sessions
    //perform append actions here
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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