Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda