DEV Community

Discussion on: 8 Best JavaScript One-Liners

Collapse
 
arielmejiadev profile image
Ariel Mejia

Hi @shivamblog If it makes sense to you I would add a one-liner to shuffle and pick items from an array:

const randomizeArray = () => [...array].sort(() => 0.5 - Math.random()).slice(0, items);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
shivams1007 profile image
Shivam Singh

what is items here?

Collapse
 
arielmejiadev profile image
Ariel Mejia

@shivamblog Sorry items could be the number of items so you can shuffle and also get only some of them:

and the array should be the array to shuffle and slice it.

const randomizeArray = () => [...array].sort(() => 0.5 - Math.random()).slice(0, numberOfItems);
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
shivams1007 profile image
Shivam Singh

Then it is correct!