Filling an array with the data of payload

Using the node node-red-contrib-ical-events to get events from my Google Calendar the output with all upcoming events is generated as shown in the graphic below. So far so good. Now I'm trying to figure out how to configure a node to have an output for only the upcoming 3 events and something like this:

Date[0] = "06-08-2024", Event[0] = "Test event 1"
Date[1] = "07-08-2024", Event[1] = "Test event 2"
Date[2] = "08-08-2024", Event[2] = "Test event 3"

I think this is called a multi dimensional array? But I can't seem to get it to work, can anyone advise me?

Looking at the picture of your debug output, I think msg.payload is an array of javascript objects, not a multidemensional array.

You can reduce it to the first 3 elements with a function node
msg.payload.length = 3
or the last 3 elements
msg.payload = msg.payload.slice(-3)

Since you posted your data in the form of a picture rather than the actual data, I have not been able to test the above suggestions.

What do you want to do with that data? What you want to do with it will help to determine what structure you want.

1 Like

Actually I only want to publish the date and the event ("summary") of 3 or 5 upcoming events with MQTT to process them further elsewhere.

Thanks for your help jbudd. I didn't place the flow because my Google Calendar credentials would be visible, I have now removed them. But maybe testing is not necessary because your solution works.

I would like to ask you if you also know how I can extract only the date (like "06-08-2024") and the summary (like "Test event 1") from the payload. I don't need the other information for my project.

[
    {
        "id": "ab9fab9d.d6fd98",
        "type": "ical-upcoming",
        "z": "87ace8dd212cde58",
        "confignode": "bec5632f59cc7b0f",
        "timeout": "10",
        "timeoutUnits": "minutes",
        "cron": "",
        "name": "Upcoming events",
        "offsettype": "",
        "offset": "",
        "offsetUnitstype": "",
        "offsetUnits": "",
        "eventtypes": "events",
        "eventtypestype": "eventtypes",
        "calendar": "",
        "calendartype": "str",
        "triggertype": "trigger",
        "trigger": "always",
        "timezone": "",
        "timezonetype": "str",
        "dateformat": "{ \"timeStyle\": \"short\", \"dateStyle\": \"short\" }",
        "dateformattype": "json",
        "language": "nl",
        "languagetype": "language",
        "filterProperty": "summary",
        "filterPropertytype": "filterProperty",
        "filterOperator": "between",
        "filterOperatortype": "filterOperator",
        "filtertype": "str",
        "filter2type": "str",
        "filter2": "",
        "filter": "",
        "checkall": false,
        "endpreview": "10",
        "endpreviewUnits": "days",
        "previewtype": "num",
        "preview": "100",
        "previewUnitstype": "previewUnits",
        "previewUnits": "days",
        "pastviewtype": "num",
        "pastview": "0",
        "pastviewUnits": "days",
        "pastviewUnitstype": "pastviewUnits",
        "x": 550,
        "y": 180,
        "wires": [
            [
                "fc613b72.7ac058",
                "290133140125f3e1",
                "1249d1958c010d1f"
            ]
        ]
    },
    {
        "id": "bec5632f59cc7b0f",
        "type": "ical-config",
        "url": "https://calendar.google.com/calendar/ical/0000000000%40gmail.com/private-0000000000000000000000000000000/basic.ics",
        "caldav": "",
        "caltype": "ical",
        "name": "Google Calendar",
        "replacedates": false,
        "usecache": false,
        "username": "",
        "password": "",
        "experimental": false,
        "calendar": "",
        "pastWeeks": "0",
        "futureWeeks": "4",
        "credentials": {}
    }
]

Something like:

const i = msg.payload.slice(-3)
msg.payload = i.map(x=>{ return { date: x.date.split(" ")[0], event:x.summary }})

return msg
1 Like

Another way to do it would be to use a change node with this JSONata expression:

payload.{
    "date": eventStart.$substringBefore("T"),
    "summary": summary
}
1 Like

It all works great. Thanks all four of you for your help, it's really great that there are people on this forum who always provide a solution.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.