兴国资源网 Design By www.nnzcdc.com

MongoDB数据库forEach语句循环遍历功能是非常常用的一个功能。
采用foreach循环遍历,并每次循环允许执行一次回调函数。
此外,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);
兴国资源网 Design By www.nnzcdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
兴国资源网 Design By www.nnzcdc.com