How to split an array into interval chunks

I have an enum like this:

enum MyEnum {
   elem1 = 100,
   elem2,
   elem3
   elem4,

   elem5 = 200
   elem6,
   elem7,

   elem8 = 300
   elem9
}

Is there an elegant way (using reduce or other array methods) to split this into separate interval chunks?

Expected result:

[ [100, 101, 102, 103] , [200, 201, 202], [300,301] ]

Answer

You can filter the number from your enum and group consecutive numbers in arrays.

enum MyEnum {
  elem1 = 100,
    elem2 = 101,
    elem3 = 102,
    elem4 = 103,

    elem5 = 200,
    elem6 = 201,
    elem7 = 202,

    elem8 = 300,
    elem9 = 301
}

Object.entries(MyEnum)
  .filter(([x]) => !isNaN(parseInt(x)))
  .map(([, key]) => MyEnum[key])
  .reduce((r, val, i, a) => {
    if (!i || val !== a[i - 1] + 1) r.push([]);
    r[r.length - 1].push(val);
    return r;
  }, []);