zoukankan      html  css  js  c++  java
  • js递归循环数组

    当我们从后台获取回来的数据是一个数组时,而且每个元素是一个对象,对象的层级不确定,需要使用递归循环遍历所有的子元素

    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": "未婚"
      },
      {
        "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);

    Talk is cheap,show me the code
  • 相关阅读:
    arcgis python 把多个MXD批量导出一个PDF
    arcgis python pdf合并
    arcgis python 列出一个表所有字段
    arcgis python 随机取部分数据
    arcgis python 删除一个数据库所有数据
    python将py文件转换为pyc
    从 10.x 到 ArcGIS Pro 的 Python 迁移
    Entity Framework 6.X实现记录执行的SQL功能
    Entity Framework 5.0系列之EF概览-三种编程方式
    Entity Framework 全面教程详解(转)
  • 原文地址:https://www.cnblogs.com/qc-one/p/11309980.html
Copyright © 2011-2022 走看看