0

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.

5
  • filter() method creates a new array instead of changing original. Have you tried to remove items from original array without using filter() method. Commented Aug 22, 2020 at 4:06
  • No I didn't but I just don't want to touch my original Array, either way I will need to force re-render or reload the page to get original Array from api. Maybe there is a Vue way of doing this, I'm pretty new to Vue! I've just saved my Array to store and added a button to reset it's set's my original array from store, it is solving my problem actually. But If there is a better way I'm waiting for it! Commented Aug 22, 2020 at 4:12
  • Why don't you use original array from store in filterByDate(d). I mean like this, this.orders = this.originalOrders.filter(). Vuex has mapState to make this easier. Commented Aug 22, 2020 at 4:25
  • Brilliant! Never think about that. This solves my problem thanks a lot! Commented Aug 22, 2020 at 4:30
  • :) You are welcome! Can you please update your post with these information because then it is easier to understand this for someone else. Commented Aug 22, 2020 at 4:33

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.