MongoDB:forEach循环遍历
时间:2014-04-07 19:04 来源: 我爱IT技术网 作者:微风
forEach
功能:循环遍历,并每次循环允许执行一次回调函数
说明:for循环的一种扩展,对比同浏览器端的forEach用法是一致的
例子:
>var arr = ["ab","cd","ef"]
>var show = function(value,index,ar){ print(value) }
>arr.forEach(show)
ab
cd
ef
附加 浏览器端的forEach例子:
//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);
本文来源 我爱IT技术网 http://www.52ij.com/jishu/4817.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
