Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda