mercredi 3 mars 2021

js shuffle array of objects while retaining some order

how to efficiently shuffle array of object looking something like this:

array = [
{from: john, requestNum: 1},
{from: jack, requestNum: 1},
{from: jill, requestNum: 1},
{from: john, requestNum: 2},
{from: jane, requestNum: 1}]

I would like to shuffle these objects by random, but requests from same name must retain their relative order. So where ever Johns request number 2 ends up in shuffled array, it must be behind its request n1.




Aucun commentaire:

Enregistrer un commentaire