0

I'm trying to get a new array of tracks that have the same genre music id that genreFilter.

So if genreFilter is 12 I need the new array have tracks with genre music id = 12. But the list of genres is other array so I think I need iterate it too and then return the track.

I'm doing the next

const list = tracks
      .map((track) =>
        track.primary_genres.music_genre_list.filter((genre) => {
          if (
            genre.music_genre &&
            genre.music_genre.music_genre_id === genreFilter
          ) {
            return track;
          }
        })
      );

But this only return genres which is obtained by filter function and doesn't return the track.

1 Answer 1

1

Since your desired output is an array of tracks, you should .filter on the tracks array instead. Inside the filter, check if .some of the music_genre_ids are equal to the genreFilter:

const list = tracks.filter(
  track => track.primary_genres.music_genre_list.some(
    genre => genre.music_genre?.music_genre_id === genreFilter
  )
);

Use optional chaining to make things concise. If you can't use optional chaining, then it'd have to be similar to what you were doing originally:

genere => (
  genre.music_genre &&
  genre.music_genre.music_genre_id === genreFilter
)
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. Thanks so much. I didn't know that I can ask for a property in line with the sign ?

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.