I am working with a dates array and need to implement a function that can filter out all the dates from either 2014 or 2015 depending on user input.
I attempted using substring() method, but it returned an array of the desired 2014 dates along with 2 undefined values. How can I modify my approach to get only the dates from 2014?
const datefunc = () => {
const date = ['2014-4-4', '2014-5-4', '2015-4-4', '2015-3-4']
const dates = date.filter((d) => {
return d.substring(0, 4) === '2014';
})
console.log(dates)
}
datefunc()