1

So I have an array of objects from json that looks like this:

array of obj

I'm generating a <ul> and for each <li> I get an ID from an API:

<ul>
  <li v-for="genre in movie.genre_ids">
    {{ genre }} // 19
  </li>
</ul>

I do not want to display the number, I would like the name of the genre, which is a sibling of the object that contains that ID.

How can I do that?

1 Answer 1

2

Convert your genres array into an object whose keys are the IDs:

computed: {
  genresFormatted() {
    const genres = {};
    this.genres.forEach(genre => {
      genres[genre.id] = genre.name;
    });
    return genres;
  }
}

Now it's much easier to get the name property when looping:

<li v-for="id in movie.genre_ids" :key="id">
  {{ genresFormatted[id] }}
</li>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.