`_.sample` function on array using JavaScript Library underscore.js
Hi Friends,
`_.sample` function on array using JavaScript Library underscore.js
`_.sample` is a utility function in Underscore.js library that allows you to retrieve a random element from an array. It provides a simple way to select a random item from a collection.
In below example, we have an array of numbers numbers. We use _.sample to retrieve a random element from the array and assign it to the variable randomNum. Each time you run the code, you will get a different random number from the array. We can also use _.sample with objects. In that case, it will return a random value from the object.
// syntax
_.sample(collection)
//example
var numbers = [1, 2, 3, 4, 5];
var randomNum = _.sample(numbers);
console.log(randomNum);
// Example output: 3 (random number selected from the array)
var obj = { name: "Alice", age: 30, city: "New York" };
var randomValue = _.sample(obj);
console.log(randomValue);
// Example output: "New York" (random selected value from the object)
#javascript #under #score #underscore #underscorejs #sample #underscoresample #samplefunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
-
59:08
UFC - Ultimate Fighting Championship
3 days agoGeorges St-Pierre, Payton Talbott, UFC 303 recap | UFC Unfiltered
96K21 -
2:12:05
Tactical Advisor
12 hours agoGun Giveaway Info & New Build Spotlight - Vault Room Live Stream - Vault Room Live Stream
74.5K24 -
1:15:27
JoBlo Originals
1 day agoFriday Night Flicks!
82.9K12 -
1:00:41
Trumpet Daily
1 day ago $6.79 earnedJoe Biden’s Demeanor Is ‘Not of This Earth’ - Trumpet Daily | July 5, 2024
100K109 -
12:05
Space Ice
1 day agoStar Wars The Acolyte Is So Good The Fans Failed The Critics - Best Movie Ever
91.6K108 -
30:57
Degenerate Plays
1 day ago $2.03 earnedDid Gaming Peak Back Then? - Stellar Blade : Part 6
90.2K1 -
8:03
Kicking Mustang
1 day ago $1.68 earnedUsing my Ghillie Suit to Sneak Attack Clueless Enemies..
79.2K17 -
2:47
Maicy Renee
1 day agoSounds Like a Terrible Night!
67.1K33 -
18:23
Crypto Crew University
1 day ago $1.08 earnedWARNING: BITCOIN ABOUT TO DO SOMETHING THAT CAUSED MEGA CRASH LAST TIME – CAN IT BE AVOIDED
64.7K27 -
2:52
Hack
22 hours agoAn unusual idea with a bottle and a can of spray paint
51.9K13