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

156
Views
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 answers
Answer question

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 Report

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 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!