L4W
L4W

Reputation: 15

Problem filtering Array in Array by date between 2 dates

So I'm just using a method to filter it's working like a charm but I'm losing my original Array when there is no date between I provided (as expected). What can I do to not lose my Array.

I want this because when a person gives a new date, filter it without a reload. But I cannot do it with empty Array..

Here is my function;

filterByDate(d) {
  this.orders = this.orders.filter(
    (element) =>
      element.order.orderdate >= d[0] &&
      element.order.orderdate <= d[1]
  );
},

d[0] is fromDate, d[1] is toDate. Thanks.

Upvotes: 0

Views: 136

Answers (1)

Thilina Lakshan
Thilina Lakshan

Reputation: 308

According to the comment section of problem,

  • filter() method doesn't modify the original array. instead, it creates a new one.

  • this.orders = this.orders.filter() will be caused to lose original array. Therefore, you need to store it in different variable before. (Ex: this.originalOrders)

  • Then this.originalOrders can be used for filtering,

filterByDate(d) {
  this.orders = this.originalOrders.filter(
    (element) =>
      element.order.orderdate >= d[0] &&
      element.order.orderdate <= d[1]
  );
},

Thanks!

Upvotes: 2

Related Questions