Remove all songs

Hello guys!

I’m having a situation similar to some extent as mentioned here: How to replace songs array? · Issue #184 · serversideup/amplitudejs · GitHub. That discussion does not seem to have reached a conclusion, so here I’m opening new thread about it.

On the click of a button, I need to remove all songs from the current queue and add a new song.

So, I tried something like:

Amplitude.getSongs().forEach((song, songIndex) => {
  Amplitude.removeSong(songIndex);
});
// addition handled later

I thought this would work, but it’s removing only the alternate songs as of now. From my understanding, Amplitude.getSongs() returns an array. On that, I’m using forEach and accessing the index of each song using the songIndex argument. I’m passing that value to Amplitude.removeSong(). Am I missing something or is this not possible at all?

I think this does the trick:

var currentQueue = [];
Amplitude.getSongs().forEach((song, songIndex) => {
  currentQueue.push(songIndex);
});
currentQueue.forEach(() => {
  Amplitude.removeSong(0);
});

Since the getSongs() gets updated for every call (is what I figured out), I first store the indices in an array and then remove the first item for the length of the array.


A shorter solution (but, probably not so efficient), would be:

while (Amplitude.getSongs().length) {
  Amplitude.removeSong(0);
};

Thanks for posting the solution! Sorry for missing this. We had a bug in Discourse where we were not getting new topics. It’s fixed now. :sweat_smile:

1 Like

No fuss. This library is already great and maintaining that + discourse + your life apart from would definitely be tough. Thanks for the heads-up though.

2 Likes