MongoDB數(shù)據(jù)庫(kù)forEach語(yǔ)句循環(huán)遍歷功能是非常常用的一個(gè)功能。
采用foreach循環(huán)遍歷,并每次循環(huán)允許執(zhí)行一次回調(diào)函數(shù)。
此外,foreach循環(huán)遍歷是for循環(huán)的一種擴(kuò)展,對(duì)比同瀏覽器端的forEach用法是一致的。
示例如下:
>var arr = ["ab","cd","ef"]>var show = function(value,index,ar){ print(value) }>arr.forEach(show)abcdef
附加--瀏覽器端的forEach例子:
//value為當(dāng)前元素值,index為當(dāng)前元素在數(shù)組中的索引,ar為數(shù)組本身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為回調(diào)函數(shù),scope為回調(diào)設(shè)置上下文環(huán)境,使回調(diào)函數(shù)的this指向scope變量,scope為可選參數(shù)letters.forEach(ShowResults,scope);
|
新聞熱點(diǎn)
疑難解答
圖片精選