Underscore.js是一個JavaScript庫,提供了許多有用的函數,即使不使用任何內置對象,也可以在很大程度上幫助編程,例如映射,過濾器,調用等。
_.iteratee() 函數是 Underscore 中的內置函數,用於生成可應用於集合中每個元素的回調。此方法支持常見回調用例的多種簡寫語法,並根據值的類型返回輸出。
用法:
_.iteratee( value, context )
參數:此方法接受上麵提到和下麵描述的兩個參數:
- value:這是規定的值。
- context:這是要使用的上下文。它是一個可選參數。
返回值:此方法根據值的類型返回輸出。
範例1:在此示例中,該方法未使用任何參數。
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://unpkg.com/underscore@1.11.0/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
// Calling iteratee method with
// no parameters
console.log(_.iteratee());
</script>
</body>
</html>
輸出:
function(n){return n}
範例2:在此示例中,為該方法提供了一個用戶定義的函數。
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://unpkg.com/underscore@1.11.0/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
// Calling iteratee method
// with its parameter
console.log(
_.iteratee(
function (num) {
return num * 4;
}
)
);
</script>
</body>
</html>
輸出:
function(num) { return num * 4; }
範例3:在此示例中,鍵值對作為參數傳遞。
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://unpkg.com/underscore@1.11.0/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
// Calling iteratee method
// with its parameter
console.log(
_.iteratee(
{ portal:'GeeksforGeeks' }, []
)
);
</script>
</body>
</html>
輸出:
function(n){return h.isMatch(n,r)}
範例4:在此示例中,將字符串作為參數傳遞。
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://unpkg.com/underscore@1.11.0/underscore-min.js">
</script>
</head>
<body>
<script>
// Calling iteratee method with
// its parameter
console.log(
_.iteratee('GfG', [])
);
</script>
</body>
</html>
輸出:
function(n){return null==n?void 0:n[r]}
相關用法
- Lodash _.iteratee()用法及代碼示例
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- Tensorflow.js tf.layers.embedding()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Underscore.js _.iteratee() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。