2 Limit 3 albums per artist, 3 tracks per album
gerrit edited this page 2026-06-24 08:47:03 +00:00
function limitData(data) {
  // 1. Group by artist
  const artists = {};
  data.forEach(item => {
    if (!artists[item.artistId]) artists[item.artistId] = [];
    artists[item.artistId].push(item);
  });

  const result = [];

  // 2. Iterate through artists, limit to 3 albums, then 3 tracks per album
  for (const artistId in artists) {
    const artistTracks = artists[artistId];
    
    // Group tracks by albumId
    const albums = {};
    artistTracks.forEach(item => {
      if (!albums[item.albumId]) albums[item.albumId] = [];
      albums[item.albumId].push(item);
    });

    // Limit albums to 3 per artist
    const limitedAlbums = Object.values(albums).slice(0, 3);

    // Limit tracks to 3 per album and add to the final array
    limitedAlbums.forEach(albumGroup => {
      const limitedTracks = albumGroup.slice(0, 3);
      result.push(...limitedTracks);
    });
  }

  return result;
}