简体   繁体   中英

How to count the number of objects created in Javascript?

I am working in JXA and trying to count the number of events in a calendar. As far as I understand each event in a calendar is an object, in the class calendar.

The example code places the objects in an array and then counts the length of the array, but that can't be the best way to do count the number of objects.

The code runs in ScriptEditor on Mac OS 10.15

var ical = Application('Calendar');
var calOne = ical.calendars[1]

arrEventsInCalOne = [];
i = 0
try {
  do {
    i++;
    arrEventsInCalOne.push(calOne.events[i]().startDate());
  } while (i < 100);
} catch (err) {
  "No value"
}

arrEventsInCalOne.length

To get the number of events in a single calendar:

Application('Calendar').calendars[0].events.length

To get the number of events in all calendars as a sum total:

Application('Calendar').calendars.events.length

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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