0

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"
2
  • do u mean?: response.data = {available_at: '2019-08-12', title: 'Test'} array isn't same that object Commented Sep 17, 2019 at 16:30
  • Yes sorry, that's correct Commented Sep 17, 2019 at 16:34

1 Answer 1

2

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);
    }
});
Sign up to request clarification or add additional context in comments.

10 Comments

if (response.data.available_at === new Date().toISOString().slice(0, 10)) { this.dates = response.data } This doesn't seem to work
available_at in console is "2019-09-17" and dumping the new Date portion just shows 2019-09-17 with no quotes
Wait, response.data.available_at prints undefined in your example. That's why it's not working
You cannot define a list like this: var data = [available_at: '2019-08-12', title: 'Test']. Is it actually an object like var data = {available_at: '2019-08-12', title: 'Test'} ?
Yes that's what I meant initially, I used the wron brackets. But response.data.available_at is returning undefined
|

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.