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
Nikko Ortiz
51 minutes agoBeing Poor Is A Choice?! - Rumble Studio LIVE
266 watching -
LIVE
Caleb Hammer
2 hours ago$60,000+ For New Boobs | Financial Audit
138 watching -
LIVE
The Big Migâ„¢
2 hours agoRet. Col. Doug Macgregor, Battlefield Legend & Author
4,626 watching -
1:45:19
Dear America
2 hours agoErica Kirk Just Announced As Turning Point CEO!! + Senate Dubs Oct 14th Charlie Kirk Day!!
111K26 -
LIVE
Benny Johnson
1 hour agoStephen Colbert DOUBLES DOWN On Jimmy Kimmel's Lies | Inside Decision to RIP Jimmy Kimmel Off Air...
6,012 watching -
LIVE
Badlands Media
7 hours agoBadlands Daily: September 19, 2025
3,898 watching -
35:08
Colion Noir
19 hours agoA Bear, an AR-15, and a Home Invasion
56.8K9 -
LIVE
Wendy Bell Radio
6 hours agoConsequence Culture
6,679 watching -
1:06:11
Crypto Power Hour
2 hours ago $1.90 earnedSelf-Custody vs Centralized Crypto Card
19.5K7 -
45:45
JULIE GREEN MINISTRIES
4 hours agoWHAT WILL HAPPEN ON SEPTEMBER 23RD PT.1
93.2K362