A
A
Alex2017-05-25 14:31:16
JavaScript
Alex, 2017-05-25 14:31:16

How to properly filter an array?

I have a component with an array and two filters: by type and by date.

export default {
  data() {
    date: null,
    type: 'photo',
    posts: [{type:'photo', date: 147852369}, /* ... */]
  }
}

So, how to implement filtering more correctly:
1. All filters in one function:
computed {
  filteredPosts() {
    return this.posts.filter(p => p.date > this.date && p.type === this.type)
  }
}

2. Separate function for each filter
computed {
  filterByType() {
    return this.filterByDate.filter(p => p.date > this.date)
  },
  filterByDate() {
    return this.posts.filter(p => p.type === this.type)
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Negwereth, 2017-05-25
@Negwereth

Apart. Just watch the code formatting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question