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

128
Views
V-for on 2 seperate object arrays

I'm trying to output 10 news from an array called stories. This array contains info regarding the story. fx title, text, URL

But I'm also trying to output data on the author from that story that is stored in another array called authors. This array contains, author id, author score etc.

How do I accomplish this in Vue.js using the v-for method.

This is what I got for now:

 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;
    }); 

  },

and the 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 answers
Answer question

0

If I understood you correctly you can create another v-for in authors v-for and filter stories for author:

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