欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

递归数组

程序员文章站 2022-07-15 08:25:24
...

递归处理数组

如果是对象的话就一直递归,一直到undefined

//请求下来的数据
 let jsons = res.data.data
            let json,arr =[]
            
            function getAllJson(jsons, type) {
            
                if (type == "" || type == undefined ) {
                    type = "json"
                }
                //遍历数组
                for(key in jsons) {
                    let k = name + key;
                    
                    if(!(jsons[key] instanceof Object)){
                        
                        if(key == "type"){
                          
                            if(jsons[key] == 2){
        
                                for(let i in jsons){
                                    if(i === 'cid'){
                                        if(window.localStorage.pcid == jsons[i]){
                                           
                                            json = jsons
                                        }
                                        return 
                                    }
                                }
                               
                            }
                            return 
                        }
                       
                    } else {
                        getAllJson(jsons[key], k); //如果是Object则递归
                    } 
                }
            };
            getAllJson(jsons, "");

            function getAllJsonOne(json, type) {
                if (type == "" || type == undefined) {
                    type = "json"
                }
                for (key in json) {
                    let k = name + key;
                    if (!(json[key] instanceof Object)) {
                      
                        if (key == "type") {
                            if (json[key] == 1) {
                             
                                for (let i in json) {
                                   
                                    let obj = {}
                                    if (i == "type") {
                                        if (json[i] == 1) {
                                           
                                            obj.cid = json.cid + "," + json.name
                                            obj.name =json.name
                                            
                                            obj.xfsje = json.xfsje
                                            obj.xfsjs = json.xfsjs

                                            arr.push(obj)
                                        }
                                    }
                                  
                                }

                            }
                            return
                        }
                        
                    } else {
                        getAllJsonOne(json[key], k); //如果是Object则递归
                    }
                }
            };
            getAllJsonOne(json, "");
            
         
相关标签: 遍历数组