I need to check if a date string is within a date range.
// Set up const dateRange = ["2020-07-30", "2020-07-20"]; let n = 0; const collection = [ { "createdOn": "2020-07-01", title: `title # ${ n++ }` }, { "createdOn": "2020-07-10", title: `title # ${ n++ }` }, { "createdOn": "2020-07-20", title: `title # ${ n++ }` }, // Include { "createdOn": "2020-07-21", title: `title # ${ n++ }` }, // Include { "createdOn": "2020-07-21", title: `title # ${ n++ }` }, // Include { "createdOn": "2020-07-30", title: `title # ${ n++ }` }, // Include { "createdOn": "2020-07-30", title: `title # ${ n++ }` }, // Include { "createdOn": "2020-07-31", title: `title # ${ n++ }` }, ]; // Solution const filter = (collection, dateRange) => { const dates = [...dateRange].sort(); // Sorts the date range: from -> to return collection.filter(item => item.createdOn >= dates[0] && item.createdOn <= dates[1]); // returns either array of objects or an empty array } console.table(filter(collection, dateRange)); ┌─────────┬──────────────┬─────────────┐ │ (index) │ createdOn │ title │ ├─────────┼──────────────┼─────────────┤ │ 0 │ '2020-07-20' │ 'title # 2' │ │ 1 │ '2020-07-21' │ 'title # 3' │ │ 2 │ '2020-07-21' │ 'title # 4' │ │ 3 │ '2020-07-30' │ 'title # 5' │ │ 4 │ '2020-07-30' │ 'title # 6' │ └─────────┴──────────────┴─────────────┘