I have an array of objects. Each object has a date property. I am trying to create a function where I append every item to a new array if the item's date is equal to today's date.
Also, I am not sure if the for loop is the most efficient approach for this, this list will never be more than a few hundred items though.
My function:
todayListItems() {
const todayItems = [];
const todayDate = moment(new Date()).format('dd-mm-YYYY');
for (let i = 0; i < myArray.length; i++) {
const itemDate = moment(myArray[i].date).format('dd-mm-YYYY');
if (itemDate === todayDate) {
todayItems.push(myArray[i]);
}
}
console.log(todayItems);
return todayItems;
}
This function runs but even if there is an item with today's date nothing will be pushed to the array.