`_.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
-
45:44
hickok45
9 hours agoSunday Shoot-a-Round # 247
17.7K37 -
3:34
Cooking with Gruel
21 hours agoBrown Butter Deviled Eggs
28.9K7 -
11:06
Bearing
5 hours agoThe Office 2024 Australian Reboot Looks GARBAGE
25.2K20 -
28:21
Lloyd And Mandy
9 hours agoThe INCREDIBLE Hack Every Online Business Owner MUST KNOW In 2024..
23.7K3 -
0:51
scoutthedoggie
1 day agoWhat's in your Northeast UZI Rob?
24.4K1 -
12:49
Misha Petrov
18 hours agoI Triggered The Furries…
47K73 -
6:22:05
Akademiks
19 hours agoNicki Minaj EXPOSES Jay Z over Finessing HER! Diddy got all his Peers Hiring Lawyers & on the RUN!
141K80 -
1:04:17
WarRoom Films
10 days agoGovernment Gangsters - WarRoom Film
328K66 -
8:02
Colion Noir
20 hours agoWow, $67 Million Spent On Mandatory Gun Buy Backs, Not One Gun Collected
121K146 -
1:27:27
Man in America
1 day ago🔴 LIVE: Diddy & the Hip Hop Cabal—Sodomy, Satan & Selling Souls EXPOSED
144K375