Lodash _.Each->forEach

2021-09-24 10:00 更新

_.forEach(collection, [iteratee=_.identity])

調(diào)用 iteratee 遍歷 collection(集合) 中的每個元素, iteratee 調(diào)用3個參數(shù): (value, index|key, collection)。 如果迭代函數(shù)(iteratee)顯式的返回 false ,迭代會提前退出。注意: 與其他"集合"方法一樣,類似于數(shù)組,對象的 "length" 屬性也會被遍歷。想避免這種情況,可以用_.forIn 或者_.forOwn 代替。

添加版本

0.1.0

別名

_.each

參數(shù)

  1. collection (Array|Object): 一個用來迭代的集合。
  2. [iteratee=_.identity] (Function): 每次迭代調(diào)用的函數(shù)。

返回

(*): 返回集合 collection。

例子

_([1, 2]).forEach(function(value) {  console.log(value);});// => Logs `1` then `2`. 
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {  console.log(key);});
// => Logs 'a' then 'b' (iteration order is not guaranteed)


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號