How to useSpread Operator in Javascript
To select the minimum and maximum dates in an array, use the spread operator to clone the array, then apply `Math.min` and Math.max with the spreaded array, respectively.
Example: In this example, we are using sort() Method.
let dates = [new Date('2022-01-01'), new Date('2022-03-15'), new Date('2022-02-10')];
let minDate = new Date(Math.min(...dates));
let maxDate = new Date(Math.max(...dates));
console.log(minDate);
console.log(maxDate);
Output
2022-01-01T00:00:00.000Z 2022-03-15T00:00:00.000Z
How to select Min/Max dates in an array using JavaScript ?
Given an array of JavaScript date. The task is to get the minimum and maximum date of the array using JavaScript.
Below are the following approaches:
Table of Content
- Using Math.max.apply() and Math.min.apply() Methods
- Using reduce() method
- Using Spread Operator
- Using Array.prototype.sort with a custom comparator