分类目录

链接

友情链接

统计信息

  • 日志总数:114篇
  • 评论总数:185条
  • 分类总数:11个
  • 标签总数:391个
  • 友情链接:0个
  • 网站运行:3177天

2019年八月
« 3月    
 1234
567891011
12131415161718
19202122232425
262728293031  
>
现在位置:    首页 > javascript > 正文

用Javascript 遍历一个多维 数组

javascript 暂无评论

这是一个 网上找到的一个 比较严谨的多维数组的方法  记录一下  以备忘

		Array.prototype.each = function(fn){                                                  
	    try{                                                                              
	        //1 目的: 遍历数组的每一项 //计数器 记录当前遍历的元素位置                                            
	        this.i || (this.i=0);  //var i = 0 ;                                          
	        //2 严谨的判断什么时候去走each核心方法                                                       
	        // 当数组的长度大于0的时候 && 传递的参数必须为函数                                                 
	        if(this.length >0 && fn.constructor == Function){                             
	            // 循环遍历数组的每一项                                                             
	            while(this.i < this.length){ //while循环的范围                              
	                //获取数组的每一项                                                            
	                var e = this[this.i];                                                 
	                //如果当前元素获取到了 并且当前元素是一个数组                                              
	                if(e && e.constructor == Array){                                      
	                    // 直接做递归操作                                                        
	                    e.each(fn);                                                       
	                } else {                                                              
	                    //如果不是数组 (那就是一个单个元素)                                              
	                    // 这的目的就是为了把数组的当前元素传递给fn函数 并让函数执行                                 
	                    //fn.apply(e,[e]);                                                
	                    fn.call(e,e);                                                     
	                }                                                                     
	                this.i++ ;                                                            
	            }                                                                         
	            this.i = null ; // 释放内存 垃圾回收机制回收变量                                        
	        }                                                                             
	                                                                                      
	    } catch(ex){                                                                      
	        // do something                                                               
	    }                                                                                 
	    return this ;                                                                     
	}                                                                                     
	       

   //使用方法
   
	var arr = [
			[1,2,[3,4,5]],
			[6,7,8],
			[9,10,11],
		];




	arr.each(function(item){                                                              
	    alert(item);                                                                      
	});

这就是javascript 多维数组的遍历方法 

本文版权归企业网站建设,做网站,网站建设,php网站建设,dedecms建站,ecshop二次开发,html空间,网站修改所有,转载引用请完整注明以下信息:
本文作者:admin
本文地址:用Javascript 遍历一个多维 数组 | 企业网站建设,做网站,网站建设,php网站建设,dedecms建站,ecshop二次开发,html空间,网站修改

抱歉!评论已关闭.