📑Array

Prototype to Array

Getting a random value

// JavaScript
const array = [...];
const value = array[Math.floor(Math.random() * array.length)]; // Only 1 random value

// djs-protofy
const array = [...];
const value = array.random(); // Only 1 random value
const value = array.random(3); // 3 random values
const value = array.random(3, true); // 3 random values, but it allows duplicate values

Shuffling an array

// JavaScript
const array = [...].sort(() => Math.random() - 0.5);

// djs-protofy
const array = [...].shuffle();

Last updated