Geoff_S
Geoff_S

Reputation: 5105

Vue, conditional for putting values into an array

I have a call in my Vue site that returns a response.data object which contains a date:

An object in response.data looks like:

response.data = {available_at: '2019-08-12', title: 'Test'}

I'm currently fetching that and building an array called dates with this.dates which works fine. But, I'm wondering if there's a way where I can say if response.data.available_at = current_date then put in this.dates, else put in this.pastDates

Is there a way to modify this to do so?

fetch() {
    axios.get('/items/')
    .then(response => {
      // handle success
      console.log(response.data)
      this.dates = response.data
    })
    .finally(function() {})
}

UPDATE: response.data (a list of objects)

(51) [{…}, {…}, {…}, {…},...]
0:
available_at: "2019-09-16"
title: "2"

Upvotes: 0

Views: 193

Answers (1)

qristjan
qristjan

Reputation: 183

Is the date a string? Maybe you can use something like this then:

if (response.data.available_at === new Date().toISOString().slice(0, 10)) {
//add to this.dates
} else {
// add to this.pastDates
}

Okay, the response.data is a list of these objects. So it would go something like this:

response.data.forEach(item => {
    if (item.available_at === new Date().toISOString().slice(0, 10)) {
        this.dates.push(item);
    } else {
        this.pastDates.push(item);
    }
});

Upvotes: 2

Related Questions