title: javascript iterating over array and feeding into another function
body: <p>I'm populating a FullCalendar.io instance and drawing the data from the Wordpress REST API on my site to do so.</p>
  <p>I am able to carry out an XHR request and return the relevant data in an array <em>myData</em>.</p>
  <p>Events are added as so:</p>
  <pre><code>var calendarEl = document.getElementById('calendar');
  
  var calendar = new FullCalendar.Calendar(calendarEl,
        {
          initialView: 'dayGridMonth',
          events: [
             {
               title: 'Event Title2',
               start: $start,
               end: '2021-04-08T13:13:55-0400',
             }
           ],
        });
        calendar.render();
        }
      });
  </code></pre>
  <p>This is in a script tag in the header and being rendered to the page to a div #calendar.</p>
  <p>$start is correctly interpolated and rendered when using <code>let $start = myData[0].acf.start;</code></p>
  <p>However, I want to iterate over <em>myData</em> and return an object, which I am able to console log, but unable to then feed into the options under <code>events:</code></p>
  <pre><code>
          function myFunction(element) {
          let x = element.acf.start;
          let y = element.acf.end;
          let title1 = element.title.rendered;
          var event = {title: title1, start: y, end: x};
        
          //return event;
  
  </code></pre>
  <p>I tried putting in a correctly formed javascript opject into the <code>events:</code> array but it doesn't work. Neither does trying to call myFunction().</p>
  <p>I had errors such as $start is not defined, so I tried moving it below to make it globally available (by calling it before the function has defined it).</p>
  <p>I want to have an object for each event correctly rendered in the events option for the calendar.</p>
  <p>How can I get all my events into the calendar and not just the latest one , eg instead of myData[0].acf.start, being able to access .acf.start for every item in the myData array and returning it to the events array?</p>
  <p>Many thanks for your help</p>