递归数组
程序员文章站
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, "");
上一篇: 洛谷p1028 数的计算
下一篇: 对标记语言的一个非常全的总结