Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Possible to have a IF/ELSE STATEMENT inside of data() return

I get data back from my API when some parameters are correct.

Now I need to check if I have some data inside of current_data or not and want to change the definition of my array as you can see below.

Actually what I have tried didn't work, so it would be a big pleasure if someone can help me out!

My error: if(current_data != null) gives me multiple errors that it's not a expected declaration.

mounted() {
  get_Data() {
    var url = "justexample/API"
    
    fetch(url)
      .then((response) => {
        return response.json();
      })
      .then((data) => {
        this.current_data = data;
      })
data() {
  return {
    current_data: [],
    //Here I want to check if current data == [] like this:

    if(current_data != null) {
      array: [{
        id: this.id +=1,
        name: "",
      }]
    } else if(current_data == null {
      array: []
    }
  }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

First of all when posting questions: what didn't work? Did you get any errors?

You can't put logic/ifs in an object returned by the data function! You probably have to put your if into a computed:

data() {
....
},
computed: {
    counter() {
        return this.current_data?.length > 0 ? 1 : 0; // if the current_data array has 0 items return 0, otherwise 1
    }
}

Then you can use the counter computed in the template: <span>{{ counter }}</span>

about 4 years ago · Juan Pablo Isaza Report

0

You can do like that:

data() {
  return {
    current_data: [],
     //Here I want to check if current data == [] like this:

    counter: null;
  }
}

mounted() {
  get_Data() {
    var url = "justexample/API"

  fetch(url)
    .then((response) => {
     return response.json();
  })
  .then((data) => {
    this.current_data = data;
    
    if(current_data == []){
      this.counter = 0
    }
    } else if(current_data != [] {
      this.counter = 1
    }
   })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!