Lodash - forEach 方法



語法

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

遍歷 collection 的元素併為每個元素呼叫 iteratee。iteratee 使用三個引數呼叫:(value, index|key, collection)。iteratee 函式可透過顯式返回 false 來提前退出遍歷。

引數

  • collection (Array|Object) - 要遍歷的集合。

  • [iteratee=_.identity] (Function) - 每進行一次遍歷呼叫的函式。

輸出

  • (*) - 返回 collection。

示例

var _ = require('lodash');
var list = [1 , 2, 3, 4, 5];
 
_.forEach(list, function(value) {
   console.log(value);
});

將以上程式儲存在 tester.js 中。執行以下命令來執行此程式。

命令

\>node tester.js

輸出

1
2
3
4
5
lodash_collection.htm
廣告
© . All rights reserved.