MongoDB數據庫forEach語句循環遍歷功能是非常常用的一個功能。
采用foreach循環遍歷,并每次循環允許執行一次回調函數。
此外,foreach循環遍歷是for循環的一種擴展,對比同瀏覽器端的forEach用法是一致的。
示例如下:
1
2
3
4
5
6
|
>var arr = [ "ab" , "cd" , "ef" ] >var show = function (value, index ,ar){ print(value) } >arr.forEach(show) ab cd ef |
附加--瀏覽器端的forEach例子:
1
2
3
4
5
6
7
8
9
10
11
12
|
//value為當前元素值, index 為當前元素在數組中的索引,ar為數組本身 functionShowResults(value, index , ar) { document.write( "value:" + value); document.write( "index: " + index ); document.write( "name: " + this. name ); document.write( " " ); } varletters = [ 'ab' , 'cd' , 'ef' ]; varscope = { name : 'scope' }; // ShowResults為回調函數,scope為回調設置上下文環境,使回調函數的this指向scope變量,scope為可選參數 letters.forEach(ShowResults,scope); |