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

215
Visualizações
Iteration with v-for by using a function which returns an array in vueJs

I have a parent component that calls a child component, see the child component below, and the parent component passes it an array of authors

Array incoming with parent component

[
   {
      "name":"James T. Kirk",
      "series":[
         {
            "title":"myserie1",
            "kind":"comedy"
         }
      ]
   },
   {
      "name":"Spock",
      "series":[
         {
            "title":"Star Trek"
            "kind":"action"
         },
         {
            "title":"The Next Generation"
            "kind":"comedy"
         }
      ]
   },
   {
      "name":"Jean-Luc Picard",
      "series":[
         
      ]
   },
   {
      "name":"Worf",
      "series":[
         
      ]
   }
]

child component

<template>
   <div v-for="(auth,i) in allAuthors" :key="auth.id" class="d-sm-flex mt-8 align-start">
      // here i call my allAuthors method
      <div>
         <div class="">
            <p>{{auth.name }}</p>
            <p>{{auth.series[i].title}}</p>
         </div>
         <div class="py-4">{{auth.series[i].kind}}</div>
      </div>
   </div>
</template>
<script>
   export default {
     name: "MyListItem",
     props: {
       users: []
     },
     data () {
       return {
         author:[]
       }
     },
     created: function() {
       this.author = this.allAuthors
       console.log(this.author);
     },
     computed:{
     },
     methods: {
       allAuthors() {
         let newArr = this.users.find(x => x.series.length === 0)
         return newArr
       }
     }
   }
</script>

I want to iterate on the child component on the table coming from the parent component by filtering the table of the parent component in a method and call this method in the v-for. I want to filter the arrays to have only an array whose series exist (different from empty) i.e. whose series property contains at least one element. Then use this array in the v-for. problem it doesn't work.

How can I do it please

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can create computed property:

Vue.component('MyListItem', {
  template: `
    <div>
      <div v-for="(auth, idx) in allAuthors" :key="idx" class="d-sm-flex mt-8 align-start">
         <div class="">
            <p>{{auth.name }}</p>
            <ul>
              <li v-for="(serie, i) in auth.series" :key="i">
                <p>{{ serie.title }}</p>
                <div class="py-4">{{ serie.kind }}</div>
              </li>
            </ul>
         </div>
     </div>
   </div>`,
  props: {
    users: {type: Array}
  },
  computed: {
    allAuthors() {
      return this.users.filter(x => x.series?.length > 0)
    }
  }
})

new Vue({
  el: '#demo',
  data() {
    return {
      items: [
       {"name":"James T. Kirk", "series":[{"title":"myserie1", "kind":"comedy"}]},
       {"name":"Spock", "series":[{"title":"Star Trek", "kind":"action"}, {"title":"The Next Generation", "kind":"comedy"}]},
       {"name":"Jean-Luc Picard"},
       {"name":"Worf", "series":[]}
      ]
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <my-list-item :users="items" />
</div>

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