0

One data set is an object of arrays of ids and another is an object of arrays of ids and names. What I'd like to do is check if the ids from the first data exist in the second data set and if they do then display the names.

This is what is being called by the component, which works correctly:

<td>Genre</td>
<td>{this.matchGenres(this.props.movie.genre_ids, this.props.genres)}</td>

And this is the function that I can't get to work:

matchGenres = (genres, genreList) => {

    genres.forEach((genre) => {
      genreList.filter((list) => {
         return list.id === genre;
      }).map((newList) => {
         return newList.name;
      });
    });

  }

It looks like the operation performs correctly and returns the right names when I console.log it! But! its not showing up in the component on render.

3 Answers 3

1

const genres = [{
  id: 1,
  name: "Jazz Music"
}, {
  id: 2,
  name: "Something"
}];

const genreList = [1, 10, 100];

matchGenres = (genres, genreList) => genres
  .filter(genre => genreList.includes(genre.id))
  .map(genre => genre.name);

const matchedGenres = matchGenres(genres, genreList);

console.log(matchedGenres);

Sign up to request clarification or add additional context in comments.

Comments

1

But! its not showing up in the component on render.

Its because your function doesn't return anything. You return inside filter and map and your function does not return anything. Also note that forEach always return undefined

You just need a minor change. Try this

let genres = ["1", "2", "3"];
let genreList = [{
  id: "2",
  name: "Two"
}, {
  id: "32",
  name: "Three"
}]

matchGenres = (genres, genreList) => {

  return genreList.filter((list) => {
    // findIndex return array index if found else return -1 if not found
    return genres.findIndex(genere => genere === list.id) > -1;
  }).map(list => list.name);

}

console.log(matchGenres(genres, genreList));

Comments

0

This is the solution that ended up working:

if (genreList.length !== 0) {
  return genres.map(genre => genreList.find(list => list.id === genre)).map((newList) => newList.name) + ',';
}

For some reason the value of GenreList, which is an array, was showing up as empty for the first couple times the function is call. Thats another problem I'll have to look at but the if statement solves for it for the time being.

3 Comments

The Answer box isn't really for asking follow up questions. Any Answer on this thread must answer the Question, not a question in an Answer. Please visit the tour and how to answer to see how Answers on Stack Overflow work.
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
@PMerlet There's an actual answer in this post. It should not be deleted due to the last sentence. If anything, it can be edited out.

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.