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
-
15:26
ColdBeer
13 hours agoTop 15 Best Games like FALLOUT 4! 2025 Edition
7 -
LIVE
LIVE WITH CHRIS'WORLD
8 hours agoLIVE WITH CHRIS’WORLD - Deals & Drama | Here’s The Truth
112 watching -
48:25
Donald Trump Jr.
3 hours agoLawless Lawfare and the Meme Wars. Interviews with Alex Swoyer & Doug Mackey | TRIGGERED Ep.262
85.6K26 -
59:01
BonginoReport
3 hours agoSydney Sweeney Makes America Hot Again & Triggers The Libs! - Hayley Caronia (Ep.99)
35.1K25 -
53:29
Candace Show Podcast
4 hours agoEXCLUSIVE! Brigitte Macron's Lawyer Has A Dark Past. Dan Bongino Speaks Out. | Candace Ep 220
58.2K135 -
26:02
Athlete & Artist Show
7 hours ago $0.21 earnedHappy Gilmore 2, Was 30 Years Worth The Wait?!
4.44K1 -
5:44
China Uncensored
9 hours agoYou DO NOT Want To Go To China
4.21K26 -
LIVE
The Mike Schwartz Show
3 hours agoTHE MIKE SCHWARTZ SHOW Evening Edition 07-28-2025
4,219 watching -
1:18:11
Redacted News
4 hours agoScotland is being DESTROYED and Neil Oliver is trying to save it
51.1K74 -
20:38
Exploring With Nug
10 hours ago $1.06 earnedMissing Tennessee Man Found Crashed in Ravine — Could He Have Been Saved!
13.2K2