• 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
  • 相关阅读:
    kettle7.0数据库迁移(MySQL迁移到Postgresql,迁移过程中自动创建表结构)
    正向代理与反向代理区别
    MySQL存储引擎相关知识点
    设计模式-装饰器模式
    设计模式-策略模式
    算法—数据结构学习笔记(二)栈
    Spring Boot2.0学习笔记(一)
    关联容器——map
    迭代器
    C风格字符串
  • 原文地址:https://www.cnblogs.com/qc-one/p/11309980.html
Copyright © 2020-2023  润新知