getRandomValue

getRandomValue

  • (collection: Collection) => any
  1. Return a random value in the collection.

    If the collection is empty, undefined is returned.

    getRandomValue(['a', 'b', 'c']) // is a, b or c
    getRandomValue(['a', 'b', 'c']) // is a, b or c
    
    getRandomValue(['a', 'b', 'c']) // is a, b or c
    getRandomValue(['a', 'b', 'c']) // is a, b or c
    
  2. Sometimes we want to pick a random value. Below, we're playing a game of Monopoly. Let's see who goes first.

    const players = ['tom', 'ken', 'chris']
    const firstPlayer = getRandomValue(players)
    console.log(firstPlayer) // is tom, ken or chris
    const players = ['tom', 'ken', 'chris']
    const firstPlayer = getRandomValue(players)
    console.log(firstPlayer) // is tom, ken or chris
    
    const players = ['tom', 'ken', 'chris']
    const firstPlayer = getRandomValue(players)
    console.log(firstPlayer) // is tom, ken or chris
    const players = ['tom', 'ken', 'chris']
    const firstPlayer = getRandomValue(players)
    console.log(firstPlayer) // is tom, ken or chris