How to use Array.prototype.reduce Method In Javascript

Another approach to select a random element from an array in JavaScript is by using the reduce method in combination with Math.random(). This method involves iterating through the array and selecting a random element based on a cumulative probability.

Example: In this example, we will use the reduce method to randomly select an element from an array.

JavaScript
function getRandomElementWithReduce(array) {
    return array.reduce((selected, item) => {
        return Math.random() < 1 / (array.length) ? item : selected;
    }, array[0]);
}

// Example usage
const sampleArray = [100, 200, 300, 400, 500];
const randomItemWithReduce = getRandomElementWithReduce(sampleArray);
console.log(randomItemWithReduce);

Output
100


JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



How to select a random element from array in JavaScript ?

We are going to learn how can we select a random element from an array in JavaScript. we will be given an array and we need to print a random element from that array.

Selecting a random element from an array is a common task, especially in scenarios where you need to introduce randomness or unpredictability into your code. Whether you’re building a game, conducting statistical analysis, or implementing a feature that requires random selection, knowing how to pick a random element from an array is important.

How to select a random element from an array in JavaScript?

These are the following approaches for solving this problem:

Table of Content

  • Using Math.random() function
  • Using custom function
  • Using Lodash _.sample method
  • Using Array.splice() with Math.random()
  • Using Array destructuring and Math.floor()

Similar Reads

Using Math.random() function

Use the “Math.random()” function to get the random number between(0-1, 1 exclusive).Multiply it by the array length to get the numbers between(0-arrayLength).Use the “Math.floor()” to get the index ranging from(0 to arrayLength-1)....

Using custom function

The random(a, b) method is used to generate a number between(a to b, b exclusive).Taking the floor value to range the numbers from (1 to array length).Subtract 1 to get the index ranging from(0 to arrayLength-1)....

Using Lodash _.sample method

In this approach we are using the third party librray.The _.sample() method takes an array as an input an returns the random element from that given array.we willuse _.sample() method to print random element from an array....

Using Array.splice() with Math.random()

In the Array.splice() with Math.random() approach, generate a random index within the array length. Use Array.splice() to remove the element at the random index and return it. This method modifies the original array....

Using Array destructuring and Math.floor()

In this approach, we generate a random index between 0 and array length – 1 using Math.random(). Then, we use array destructuring to directly extract the element at that random index....

Using Array.prototype.reduce Method

Another approach to select a random element from an array in JavaScript is by using the reduce method in combination with Math.random(). This method involves iterating through the array and selecting a random element based on a cumulative probability....