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

130
Visualizações
V-for en 2 matrices de objetos separados

Estoy tratando de generar 10 noticias de una matriz llamada historias. Esta matriz contiene información sobre la historia. título fx, texto, URL

Pero también estoy tratando de generar datos sobre el autor de esa historia que se almacena en otra matriz llamada autores. Esta matriz contiene la identificación del autor, la puntuación del autor, etc.

¿Cómo logro esto en Vue.js usando el método v-for ?

Esto es lo que tengo por ahora:

 created: function (){ axios.get('https://hacker-news.firebaseio.com/v0/topstories.json') .then((response) => { let results = response.data.slice(0,10) results.forEach(id => { axios.get('https://hacker-news.firebaseio.com/v0/item/' + id + '.json') .then((response) => { this.stories.push(response); let myAuthor = response.data.by; axios.get('https://hacker-news.firebaseio.com/v0/user/' + myAuthor + '.json') .then((myAuthors) => { this.authors.push(myAuthors); }) .catch((err) => { this.err = err; alert("Error fetching authors " + err); }) }) .catch((err) => { this.err = err; }) }) }) .catch((err) => { this.err = err; }); },

y el HTML:

 <template> <div class="container"> <div class="storyContainer" v-for="story in sorted_stories" :key="story"> <span class="score">{{ story.data.score }}</span> <h2>Karma: {{ story.data.karma }}</h2> <a href="{ path: '/story/' + story.data.id }">{{ story.data.title }}<span>{{ story.data.url }}</span></a><br/> <span class="meta"> by {{ story.data.by }} | {{ story.data.time }} | Story ID: {{ story.data.id }} </span><br> </div> <div v-for="author in authors" :key="author"> <h1>ID {{ author.data.id }}</h1> <h1>Karma {{ author.data.karma }}</h1> </div> </div> </template>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Si te entendí bien puedes crear otro v-for en autores v-for y filtrar historias por autor:

 new Vue({ el: '#demo', data() { return { stories: [], authors: [] } }, methods: { storiesForAuthor(id) { // 👈 method to filter stories for author id return this.stories.filter(s => s.data.by === id) } }, created: function (){ axios.get('https://hacker-news.firebaseio.com/v0/topstories.json') .then((response) => { let results = response.data.slice(0,10) results.forEach(id => { axios.get('https://hacker-news.firebaseio.com/v0/item/' + id + '.json') .then((response) => { this.stories.push(response); let myAuthor = response.data.by; axios.get('https://hacker-news.firebaseio.com/v0/user/' + myAuthor + '.json') .then((myAuthors) => { this.authors.push(myAuthors); }) .catch((err) => { this.err = err; alert("Error fetching authors " + err); }) }) .catch((err) => { this.err = err; }) }) }) .catch((err) => { this.err = err; }); }, })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.26.1/axios.min.js" integrity="sha512-bPh3uwgU5qEMipS/VOmRqynnMXGGSRv+72H/N260MQeXZIK4PG48401Bsby9Nq5P5fz7hy5UGNmC/W1Z51h2GQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <div id="demo"> <div class="container"> <div v-for="author in authors" :key="author.id"> <h3>ID {{ author.data.id }}</h3> <h5>Karma {{ author.data.karma }}</h5> <!-- 👇 inner v-for for filtering stories --> <div class="storyContainer" v-for="story in storiesForAuthor(author.data.id)" :key="story.id"> <span class="score">{{ story.data.score }}</span> <h2>Karma: {{ story.data.karma }}</h2> <a href="{ path: '/story/' + story.data.id }">{{ story.data.title }}<span>{{ story.data.url }}</span></a><br/> <span class="meta"> by {{ story.data.by }} | {{ story.data.time }} | Story ID: {{ story.data.id }} </span><br> </div> <hr /> </div> </div> </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