• 合并 && 还原属性链


    效果

    原数据
    {
      "id": 10,
      "text": {
        "title": "title",
        "content": "content"
      },
      "image": {
        "url": "image url",
        "name": "image name",
        "size": "900KB"
      }
    }
    1.还原(拆解)属性链
    [
      {
        "key": "id",
        "value": 10
      },
      {
        "key": "text.title",
        "value": "title"
      },
      {
        "key": "text.content",
        "value": "content"
      },
      {
        "key": "image.url",
        "value": "image url"
      },
      {
        "key": "image.name",
        "value": "image name"
      },
      {
        "key": "image.size",
        "value": "900KB"
      }
    ]
    2.合并属性链
    {
      "id": 10,
      "text": {
        "title": "title",
        "content": "content"
      },
      "image": {
        "url": "image url",
        "name": "image name",
        "size": "900KB"
      }
    }
    

    关键代码

    // 合并属性链
    Merge(items): any {
        var res = {}    
        for (let i = 0; i < items.length; i++) {
            var item = items[i];
            // key
            var prochains: string[] = item.key.split('.');//属性链
            var tmpObj = res;
            //组织属性
            for (let i = 0; i < prochains.length; i++) {
                const pro = prochains[i];        
                var islast = i == prochains.length - 1;
                //没有属性就创建为obj
                if (!tmpObj.hasOwnProperty(pro)) {
                    tmpObj[pro] = {}
                }
                else {
                    //检查一下,如果已经有属性了, 并且现在不是最后一个属性, 那么这个地方应该是obj,不是的话说明属性链有问题
                    if(typeof tmpObj[pro]!=="object"&&!islast){
                        tmpObj[pro] = {}            
                    }
                }
                //属性链最后一个要赋值
                if (islast) {
                    // value
                    tmpObj[pro] = item.value;
                }
                //属性下钻
                tmpObj = tmpObj[pro]
            }
        }
        return res;
    }
    // 还原属性链
    objPro2ProDic(obj, dic, root) {
        if (!dic) {
            dic = [];
        }
        for (const key in obj) {
            if (obj.hasOwnProperty(key)) {
                const ele = obj[key];
                if (typeof ele === "object" && !(ele instanceof Array)) {
                    //下钻
                    this.objPro2ProDic(ele, dic,key)
                }
                else{
                    var _key = root?`${root}.${key}`:key;
                    // 这里返回key-value
                    dic.push({
                        key:_key,
                        value:ele
                    });
                }
            }
        }
        return dic;
    }
    

    示例代码

    示例代码

  • 相关阅读:
    iOS 数字滚动 类似于老
    iOS 实现转盘的效果
    iOS 摇一摇的功能
    APP上架证书无效:解决
    iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿
    ios 获取字符串所需要占用的label的高度
    适配----Autolayout
    OC中 block 的用法
    微信小程序如何播放腾讯视频?
    IOS-UICollectionView
  • 原文地址:https://www.cnblogs.com/Lulus/p/9483020.html
Copyright © 2020-2023  润新知