Underscore.js _.sample()函數用於找出數組中存在哪些類型的元素。它給出數組的隨機元素作為輸出。我們甚至可以傳遞第二個參數來從數組中返回該數量的隨機元素。
用法:
_.sample(list, [n])
參數:
- list: 此函數將測試列表或數組。
- n: 它是函數將返回的元素的數量。
返回值:
它從傳遞的數組中返回指定數量的隨機元素,或者默認情況下僅返回一個隨機元素。
將數字列表傳遞給 _.sample() 函數
._sample() 函數使用隨機函數,然後將列表中的該元素顯示為結果。如果沒有提到第二個參數,則將采用 t 的默認值,即 1。因此,將顯示任何一個元素。
例子:下麵的代碼示例使用數字數組實現 _.sample() 函數,而不傳遞第二個參數。
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
console.log(_.sample([1, 2, 3, 4, 5, 6]));
</script>
</body>
</html>
輸出:
將第二個參數傳遞給 _.sample() 函數:
如果我們傳遞第二個參數,那麽 _.sample() 函數將從傳遞的列表中返回與前麵提到的一樣多的元素。結果將是一個數組,其中包含第二個參數中存在的元素數量。
例子:下麵的代碼將通過傳遞第二個參數來解釋 _.sample() 函數的使用。
html
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
console.log(_.sample([1, 2, 3, 4, 5, 6], 3));
</script>
</body>
</html>
輸出:
將結構傳遞給 _.sample() 函數:
我們甚至可以將結構傳遞給 _.sample() 函數,它會以相同的方式工作。它將隨機顯示結構的任何元素作為輸出。由於沒有提到第二個參數,因此結果中將隻有傳遞列表的一個元素及其所有屬性。
例子:下麵的代碼將通過傳遞結構作為參數來說明 _.sample() 函數的使用。
html
<!DOCTYPE html>
<html>
<head>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
const people = [
{
"name": "sakshi",
"hasLong": "false"
},
{
"name": "aishwarya",
"hasLong": "true"
},
{
"name": "akansha",
"hasLong": "true"
},
{
"name": "preeti",
"hasLong": "true"
}
]
console.log(_.sample(people));
</script>
</body>
</html>
輸出:
將隻有一個屬性的結構體一起傳遞給 _.sample() 函數:
如果我們傳遞一個僅具有一個屬性的結構,那麽它將以相同的方式工作,並從傳遞的結構中隨機顯示任何一個元素。這裏同樣,由於沒有提到第二個參數,因此生成的數組將僅包含一個元素。
例子:以下代碼將使用 _.sample() 函數以及僅具有一個屬性的結構。
html
<!DOCTYPE html>
<html>
<head>
<script src =
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
const users = [
{"num":"10"},
{"num":"9"},
{"num":"8"},
{"num":"7"},
{"num":"6"}
];
console.log(_.sample(users));
</script>
</body>
</html>
輸出:
相關用法
- underscore.js _.size()用法及代碼示例
- underscore.js _.second()用法及代碼示例
- underscore.js _.splitAt()用法及代碼示例
- underscore.js _.splat()用法及代碼示例
- underscore.js _.seq()用法及代碼示例
- underscore.js _.selectKeys()用法及代碼示例
- underscore.js _.snapshot()用法及代碼示例
- underscore.js _.strContains()用法及代碼示例
- underscore.js _.sneq()用法及代碼示例
- underscore.js _.sub()用法及代碼示例
- underscore.js _.sortedindex()用法及代碼示例
- underscore.js _.sortBy用法及代碼示例
- underscore.js _.some用法及代碼示例
- underscore.js _.shuffle用法及代碼示例
- underscore.js _.size用法及代碼示例
- underscore.js _.delay()用法及代碼示例
- underscore.js _.difference()用法及代碼示例
- underscore.js _.flatten()用法及代碼示例
- underscore.js _.initial()用法及代碼示例
- underscore.js _.zip()用法及代碼示例
- underscore.js _.wrap()用法及代碼示例
- underscore.js _.without()用法及代碼示例
- underscore.js _.last()用法及代碼示例
- underscore.js _.isRegExp()用法及代碼示例
- underscore.js _.union()用法及代碼示例
注:本文由純淨天空篩選整理自Sakshi98大神的英文原創作品 Underscore.js _.sample() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。