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

167
Vistas
v-si la función no devuelve nada, pero debería

Me encontré con un problema que me ha estado molestando durante las últimas 2 horas.

Tengo una función en un mixin así:

 checkPermissionMeetings(type,areaID){ if(this.$store.state.global.permissions.meetings===true) { //If user has basic meeting access if(type==='write'){ Object.entries(this.$store.state.global.permissions.meeting_areas).forEach(([key, val]) => { if (parseInt(key) === parseInt(areaID) && (val === 'write')) { console.log('write_true') return true; } }); } if(type==='read'){ Object.entries(this.$store.state.global.permissions.meeting_areas).forEach(([key, val]) => { if (parseInt(key) === parseInt(areaID) && (val === 'read' || val === 'write')) { console.log('read_true') return true; } }); } } },

Lo llamo en un componente de vista que representa una barra de navegación:

 <span v-for="area in area_meetings"> {{area.id}} {{$store.state.global.permissions.meeting_areas[area.id]}} //This is here so I can see that I am sending the correct values to the function {{checkPermissionMeetings('read', area.id)}} //This is here so I can see the output, but it returns nothing <router-link tag="v-list-item" link :to="'/meetings/'+area.id" class="ml-5" v-if="checkPermissionMeetings('read', area.id)"> <v-list-item-icon> <v-icon>mdi-chair-rolling</v-icon> </v-list-item-icon> <v-list-item-title>{{area.name}}</v-list-item-title> </router-link> </span>

He registrado en la consola las salidas como puede ver en el código, y se ven bien. También puse la función entre llaves para poder ver el retorno, pero esto no devuelve nada en absoluto.

esta es una imagen

Puedo verlos en mi tienda

esta es una imagen

Incluso los estoy haciendo eco en el render y los valores coinciden...

esta es una imagen

¿Hay algo fundamental que me estoy perdiendo aquí?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Como se afirma en otras respuestas, usar for funcionaría, pero describiré por qué no funcionará:

 function functionWithForEach(arr) { arr.forEach(a=>{ if(a == "matched"){ return "horrah"; } }) // here it is anonymous function //The return exits the current function which is anonymous so it doesn't get returned } function functionWithForOf(arr){ for(let a of arr){ if(a == "matched"){ return "horrah"; // here it is not anonymous function } } } function functionWithPlainFor(arr){ for(let index =0; index < arr.length; index++){ if(arr[index]=="matched"){ return "horrah"; } } } function functionWithForEachVariable(arr) { let wordMatched = ""; arr.forEach(a=>{ if(a == "matched"){ wordMatched = "horrah"; return "horrah"; } } ) // see how the return does let code below it to execute console.log("I will be called no matter return is called inside forEach"); return wordMatched; } let matchingArr = ["mat","matched","not correct"]; console.log(functionWithForEach(matchingArr),"from forEach"); console.log(functionWithForOf(matchingArr),"from for-of"); console.log(functionWithPlainFor(matchingArr),"from plain for"); console.log(functionWithForEachVariable(matchingArr),"from forEach with Variable");

Está claro a partir de estos que no podemos usar forEach en esta situación

Si quieres saber más sobre estos:

¿Qué significa la palabra clave `return` dentro de la función `forEach`?

Cortocircuito Array.forEach como llamar a romper

¿Por qué este forEach devuelve indefinido cuando se usa una declaración de devolución?

about 4 years ago · Juan Pablo Isaza Denunciar

0

Debido a que return al método forEach , su función principal no devuelve nada. Puede intentar guardar algunos datos devueltos por variables y luego if las declaraciones lo devuelven. O una forma más fácil de usar otro bucle como for of

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