Premium Only Content

`_.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
-
LIVE
The Bubba Army
2 days agoCharlie Kirk's Shooter, Had a Trans BF? - Bubba the Love Sponge® Show | 9/15/25
2,959 watching -
30:31
DeVory Darkins
14 hours ago $4.73 earnedCNN forced to admit the shocking truth about shooter as Fetterman drops bomb on Democrats
6.86K60 -
20:54
Professor Nez
15 hours ago🚨BREAKING: Shooter’s Own Words in Charlie Kirk Case Revealed
1.79K3 -
16:48
itsSeanDaniel
15 hours agoGenius Republican Defends Charlie Kirk and CALLS OUT The Radical Left
2641 -
9:09
Freedom Frontline
15 hours agoFox News Just Showed The Video Biden Never Wanted Seen
4.13K13 -
0:27
historyofboxing
17 hours ago $0.13 earnedThe Cuban Machine… and Deadliest KO Phenom Right Now – David Morrell Jr
1.48K -
38:07
Degenerate Plays
14 hours ago $0.24 earnedWorst Star Wars Character Pranks Us All - Call of Duty: Modern Warfare 3 (2011) : Part 3
3.66K -
LIVE
BEK TV
2 days agoTrent Loos in the Morning - 9/15/2025
169 watching -
3:38:49
Badlands Media
1 day agoThe Narrative Ep. 38: The Sovereign World
118K62 -
2:57:44
The Charlie Kirk Show
13 hours agoWASHINGTON D.C. PRAYER VIGIL FOR CHARLIE KIRK
266K451