1.forEach,map,filter三个函数者是相同的调用参数。(callback[, thisArg])

callback is invoked with three arguments:

  • the element value
  • the element index
  • the array being traversed
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (fn, thisObj) {
var scope = thisObj || window;
for (var i = 0, j = this.length; i < j; ++i) {
fn.call(scope, this[i], i, this);
}
};
} if (!Array.prototype.map) {
Array.prototype.map = function (fn, thisObj) {
var scope = thisObj || window; var res = [];//区别在于这里,forEach不会生成新的数组 for (var i = 0, j = this.length; i < j; ++i) {
res[i] = fn.call(scope, this[i], i, this);
}
return res;
};
} if (!Array.prototype.filter) {
Array.prototype.filter = function (fn, thisObj) {
var scope = thisObj || window;
var a = [];
for (var i = 0, j = this.length; i < j; ++i) {
if (!fn.call(scope, this[i], i, this)) {
continue;
}
a.push(this[i]);
}
return a;
};
}

2.示例

function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
// a[0] = 2
// a[1] = 5
// a[2] = 9
var map = Array.prototype.map
var a = map.call("Hello World", function(x) { return x.charCodeAt(0); })
// a now equals [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
/* roots is now [1, 2, 3], numbers is still [1, 4, 9] */
function isBigEnough(element, index, array) {
return (element >= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]

最新文章

  1. iis 7.5 0x80004005 静态文件 html、js、css 500错误
  2. 搭建高可用mongodb集群(二)—— 副本集
  3. java执行效率低,但效率就低吗?
  4. jetty
  5. 后端码农谈前端(CSS篇)第三课:选择器
  6. 本地预览图片html和js例子
  7. 通过jekyll建立静态网页
  8. 从数据包谈如何封杀P2SP类软件
  9. 关于json的理解
  10. MYSQL 备份工具
  11. 绫致时装讲述O2O细节:野心在“私人定制” - 移动购物 - 亿邦动力网
  12. BZOJ-1050-[HAOI2006]旅行comf(并查集)
  13. micropython TPYBoard v202 超声波测距
  14. 数论:px+py 不能表示的最大数为pq-p-q的证明
  15. Linux之软链接与硬链接
  16. 基于Nginx和Zookeeper实现Dubbo的分布式服务
  17. Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday
  18. python的几个小程序
  19. 转载:小结(1.7)《深入理解Nginx》(陶辉)
  20. ajax简单手写了一个猜拳游戏

热门文章

  1. socket 中文man页面函数
  2. React Native(六)——PureComponent VS Component
  3. Python进阶 学习笔记(二)
  4. 使用VS Code写PHP并进行调试
  5. Tomorrow Is A New Day
  6. c++ istream转换为std::string
  7. 3944: Sum[杜教筛]
  8. Notepad++如何关闭最近打开的文件的历史记录功能
  9. python WEB UI自动化在日期框中动态输入当前日期
  10. 【转】RTMP/RTP/RTSP/RTCP协议对比与区别介绍