Filter not working as intended: filter()

2022 visit: it’s fun seeing where I started, now JavaScript is my best friend.

I doubt this post will be useful to you though, as some bit of code was in a Pastebin that is now deleted.

Hello there!

I’m not sure my filter work as intended, the command selects 9 names at random and the latest name from the list, and supposedly filters out the names that have already been randomly selected to avoid duplicates, yet I still have duplicates from time to time. Where did I mess up?

$(eval var u="$(urlfetch json".split(",");u.pop();var t=(u.length)+1;var l=u.pop();var s=[];for(i=0;i<9;i++){u.filter(x=>!s);s[i]=u[Math.floor(Math.random()*u.length)];}s=s.join(", ");"Here\'s a small selection of streamer\'s kids: "+s+", and "+l+". Total: "+t+" kids. Consult the full list here:";)

Thanks in advance!


Instead of using filter(), you can use the array splice() method to remove elements from an array at a given index:

$(eval a=`$(urlfetch json`.split(`,`);a.pop();d=a.length;b=[];c=a.pop();for(i=0;i<9;i++){b.push(a.splice(Math.floor(Math.random()*a.length),1));}`Here's a small selection of streamer's kids: ${b.join(`, `)}, and ${c}. Total: ${d} kids. Consult the full list here:`)
1 Like

Oh wow, that’s a very smart way to deal with it, I learned something new today, thank you very much! :heart:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.