I saw this question being asked a lot but i have not been able to solve it yet, so here goes! I'm working on a project where i display a list of orders. I want to be able to filter the orders by date. Therefore i'm trying to push each filtered order into a new array, but this gives me the following error:
"TypeError: filteredOrders.push is not a function"
I've tried to tweak my push syntax as i thought i had it wrong but to no avail. Each order is returned as an Object. How can i push these objects into an Array? The orders array i am looping through is also a array of objects. Here's my code for a more clear example:
data: function () {
return {
isFilterShown: false,
IsOrderDetailShown: false,
selectedDateFrom: new Date(),
selectedDateTill: new Date(),
}
},
methods: {
applyFilter: function () {
let dateFrom = this.selectedDateFrom;
let dateTill = this.selectedDateTill;
let filteredOrders = Array;
this.orders.forEach(function (order) {
if (order.createdAt >= moment(dateFrom).format('DD-MM-Y') && order.createdAt <= moment(dateTill).format('DD-MM-Y')) {
console.log(order.createdAt, order);
return order;
}
filteredOrders.push(order);
})
// this.orders = this.filteredOrders;
this.isFilterShown = false;
},

let filteredOrders = []. (Which is more canonical thannew Array()in this usage.) Similar to how you correctly set the dates--by creating an instance.