美文网首页
js递归循环数组

js递归循环数组

作者: 最好的自己_3d47 | 来源:发表于2019-09-26 15:28 被阅读0次
    从后台获取回来的数据是一个数组时,而且每个元素是一个对象,对象的层级不确定,就需要使用递归循环遍历所有子元素
    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);
    

    相关文章

      网友评论

          本文标题:js递归循环数组

          本文链接:https://www.haomeiwen.com/subject/yxpsyctx.html