zoukankan      html  css  js  c++  java
  • 深层对象转深层数组(重点:先把对象转数组,直接for in 遍历对象不太可行)

    var json: {
                PRow0: { 
                    style: { 
                         10 
                    } 
                },
                PTable1: { 
                    style: { 
                        height: 20 
                    } 
                }
            }
    
    const jsonToArrTree = (json) => {
        let data = Object.keys(json).map( (item) =>  {
            let ops = {
                title:item,
                key:nanoid(),
                children:[]
            }
            let child = json[item]
            if(typeof child == "object"  && Object.keys(child).length > 0){
                ops.children = jsonToArrTree(child)
            }
            return ops
        })
        return data
    }
    
    转换结果 [ { "title": "PRow0", "key": "LzgVGHyEkwyUAUVOSOt_u", "children": [ { "title": "style", "key": "0KNgtu0C4Cg_8QalqAcTm", "children": [ { "title": "width", "key": "jN4HAiWnfFXNz4csrtEQz", "children": [] } ] } ] }, { "title": "PTable1", "key": "Y4i4tq8C12rXbH_GBtAXl", "children": [ { "title": "style", "key": "2bD_q7ZfEu3yZILLsUfSn", "children": [ { "title": "height", "key": "OOg79AeMP-BQoDmF7wzlX", "children": [] } ] } ] } ]

      

    每个字段都是上一级父级字段的拼接

    let pre = '' const jsonToArrTree = (json) => { let data = Object.keys(json).map( (item) => { pre += (pre ? '.':'') + item let ops = { title:pre, key:nanoid(), children:[] } let child = json[item] console.log(pre) if(typeof child == "object" && Object.keys(child).length > 0){ ops.children = jsonToArrTree(child) } return ops }) pre = '' return data }
  • 相关阅读:
    手机如何当电脑的摄像头使用
    内网穿透软件
    如何在laravel框架中使用阿里云的oss
    css position 定位详解
    laravel 速查表
    window10如何查看连接过的wifi密码
    sweetalert弹出层组件
    phpstudy安装 与phpstudy_Windows web面板安装
    程序员修炼之道读后感
    JAVA-WEB-简单的四则运算
  • 原文地址:https://www.cnblogs.com/winyh/p/11772891.html
Copyright © 2011-2022 走看看