skip to Main Content

I have an object in the following format:

const input = {
  "recurrence": {
    "rrule": [
      "RRULE:FREQ=DAILY;UNTIL=20230606T133000Z"
    ],
  }
};

I want to extract the value of FREQ to check if its value is DAILY or YEARLY and to get the value of UNTIL as well.

2

Answers


  1. Your code may look like this:

    const
       input = {
        "recurrence": {
          "rrule": [
            "RRULE:FREQ=DAILY;UNTIL=20230606T133000Z"
          ],
        }
      },
      parts = input.recurrence.rrule[0].split(';'),
      isDaily = parts[0].split('=')[1] === 'DAILY',
      untilValue = parts[1].split('=')[1];
    
    console.log({
      isDaily,
      untilValue
    });
    Login or Signup to reply.
  2. Using regex here to extract the value of FREQ and UNTIL from string.

    const input = {
      "recurrence": {
        "rrule": [
          "RRULE:FREQ=DAILY;UNTIL=20230606T133000Z"
        ],
      }
    };
    
    const rrule = input.recurrence.rrule[0];
    const freqMatch = rrule.match(/FREQ=([^;]+)/);
    const untilMatch = rrule.match(/UNTIL=([^;]+)/);
    
    if (freqMatch) {
      const freqValue = freqMatch[1];
      if (freqValue === "DAILY") {
        console.log("Frequency is DAILY");
      } else if (freqValue === "YEARLY") {
        console.log("Frequency is YEARLY");
      }
    }
    
    if (untilMatch) {
      const untilValue = untilMatch[1];
      console.log("UNTIL value:", untilValue);
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search