简体   繁体   English

如何计算在 Javascript 中创建的对象数量?

[英]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.我在 JXA 工作并试图计算日历中的事件数。 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代码在 Mac OS 10.15 上的 ScriptEditor 中运行

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

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

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