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

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

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