首页 > 其他 > 详细

forEach方法的实现

时间:2017-04-09 14:29:14      阅读:222      评论:0      收藏:0      [点我收藏+]
 1 var arr = [1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5];
 2 Array.prototype.forEach = Array.prototype.forEach || function(callback, thisArg) {
 3         if (!callback || typeof callback !== ‘function‘) return;
 4         for (var i = 0, j = this.length; i < j; i++) {
 5             callback.call(thisArg, this[i], i, this);
 6         }
 7 }
 8 
 9 /*
10    forEach方法实现数组去重 
11 */
12 
13 var newArr = [];
14 arr.forEach(function(ele,index,arr){
15     if(arr.indexOf(ele)===index){
16         newArr.push(ele);
17     }
18 })
19 console.log(newArr);

 

forEach方法的实现

原文:http://www.cnblogs.com/guangyan/p/6684649.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!