js递归循环数组
2019-09-26 本文已影响0人
最好的自己_3d47
从后台获取回来的数据是一个数组时,而且每个元素是一个对象,对象的层级不确定,就需要使用递归循环遍历所有子元素
var tdata=[
{
"code": "has Value Domain Member",
"value": "未婚",
"properties": [
{
"code": "MDM VD Member PKID",
"value": "4055",
"properties": [
{
"code": "MDM VD Member PKID",
"value": "4055",
"properties": []
}
]
}
]
},
{
"code": "MDM VD Member Name",
"value": "未婚",
"properties": [
{
"code": "MDM VD Member PKID",
"value": "4055",
"properties": []
}
]
},
{
"code": "MDM VD Member ObjectID",
"value": "10"
},
{
"code": "MDM VD Member Code",
"value": "10"
},
{
"code": "MDM VD Member RefStandard",
"value": "GB/T 2261.2-2003 个人基本信息分类与代码 第2部分: 婚姻状况代码"
},
{
"code": "VD Member Sequence",
"value": "1"
}
];
function func(tdata,resData){
if(Array.isArray(tdata) && tdata.length>0){
tdata.forEach(function(v,i){
var newValue=v.code+":"+v.value;
resData[i]={};
resData[i].label=newValue;
var arr=[];
func(v.properties,arr);
resData[i].children=arr;
});
}
}
var resArr=[];
func(tdata,resArr);