What are ES2024 - ES15 features?

1. Object.groupBy()

The Object.groupBy() function allows grouping object elements based on values returned by a callback function. This is especially useful for organizing data intuitively.

const fruits = [
{ name: 'apple', quantity: 10 },
{ name: 'banana', quantity: 20 },
{ name: 'cherry', quantity: 10 }
];

const groupedByQuantity = Object.groupBy(fruits, fruit => fruit.quantity);
console.log(groupedByQuantity);
// {
// '10': [{ name: 'apple', quantity: 10 }, { name: 'cherry', quantity: 10 }],
// '20': [{ name: 'banana', quantity: 20 }]
// }

2. Map.groupBy()

Map.groupBy() is similar to Object.groupBy(), but for maps. It allows grouping map elements based on values returned by a callback function, providing additional flexibility when working with more complex data sets.

const fruits = new Map([
['apple', 10],
['banana', 20],
['cherry', 10]
]);

const groupedByQuantity = Map.groupBy(fruits, ([, quantity]) => quantity);
console.log(groupedByQuantity);
// Map {
// 10 => Map { 'apple' => 10, 'cherry' => 10 },
// 20 => Map { 'banana' => 20 }
// }

3. Temporal API

ES2024 introduces the Temporal API, a series of new objects for handling dates and times more intuitively and accurately. The Temporal API is a significant improvement over existing date and time objects, providing a more robust and flexible way to work with these data.

Temporal.PlainDate

const date = Temporal.PlainDate.from('2024-06-30');
console.log(date.year); // 2024
console.log(date.month); // 6
console.log(date.day); // 30

Temporal.PlainTime

const time = new Temporal.PlainTime(14, 30);
console.log(time.hour); // 14
console.log(time.minute); // 30

Temporal.PlainMonthDay

const monthDay = new Temporal.PlainMonthDay(5, 1);
console.log(monthDay.month); // 5
console.log(monthDay.day); // 1

Temporal.PlainYearMonth

const yearMonth = new Temporal.PlainYearMonth(2024, 6);
console.log(yearMonth.year); // 2024
console.log(yearMonth.month); // 6