jstree数据格式利用Map处理使用示例

改变之前的数据like:

let data3 = [
{ 
  "id": 23,
  "createdAt": "1613701572",
  "updateAt": "1622515678",
  "name": "vmhost",
  "config": { "-k8s": "no" },
  "desc": "",
  "type": [{ "id": 72, "createdAt": "1632906358", "updateAt": "1632906358", "name": "bxdev.node", "config": {}, "roleId": 0, "desc": "" }] 
}, 
{ 
  "id": 30, 
  "createdAt": "1613778066", 
  "updateAt": "1613778066", 
  "name": "base", "config": { "-k8s": "yes" }, 
  "desc": "", 
  "type": [{ "id": 71, "createdAt": "1632906358", "updateAt": "1632906358", "name": "bxdev.node", "config": {}, "roleId": 0, "desc": "" }] 
}, 
{ "id": 31, "createdAt": "1613778073", "updateAt": "1613778073", "name": "cph", "config": {}, "desc": "", "type": [] }, 
{ "id": 32, "createdAt": "1613778089", "updateAt": "1622515686", "name": "kubernetes", "config": { "-k8s": "no" }, "desc": "", "type": [{ "id": 73, "createdAt": "1632906358", "updateAt": "1632906358", "name": "bxdev.node", "config": {}, "roleId": 0, "desc": "" }] }, 
{ "id": 34, "createdAt": "1613788589", "updateAt": "1613788589", "name": "jumpbox", "config": {}, "desc": "", "type": [] }, { "id": 44, "createdAt": "1633923106", "updateAt": "1634528941", "name": "test", "config": {}, "desc": "newDesc", "type": [] }
]

改变之后的数据like:

treeData = [
  {
    'text': 'root',
    'icon': 'fa fa-tree',
    'state': {
      'opened': true,
      'selected': true
    },
    'children': [
      {
        'text': 'vmhost',
        'icon': 'fa fa-tree',
        'state': {
          'opened': false
        },
        'children': [
          {
            'text': 'bxdev.node',
            'icon': 'fa fa-leaf'
          }
        ]
      },
      {
        'text': 'base',
        'icon': 'fa fa-tree',
        'state': {
          'opened': false
        },
        'children': [
          {
            'text': 'bxdev.node',
            'icon': 'fa fa-leaf'
          }
        ]
      },
      {
        'text': 'cph',
        'icon': 'fa fa-tree',
        'state': {
          'opened': false
        },
        'children': [
          {
            'text': 'bxdev.node',
            'icon': 'fa fa-leaf'
          }
        ]
      },
      {
        'text': 'kubernetes',
        'icon': 'fa fa-tree',
        'state': {
          'opened': false
        },
        'children': [
          {
            'text': 'bxdev.node',
            'icon': 'fa fa-leaf'
          }
        ]
      },
      {
        'text': 'jumpbox',
        'icon': 'fa fa-tree',
        'state': {
          'opened': false
        },
        'children': [
          {
            'text': 'bxdev.node',
            'icon': 'fa fa-leaf'
          }
        ]
      },
      {
        'text': 'test',
        'icon': 'fa fa-tree',
        'state': {
          'opened': false
        },
        'children': [
          {
            'text': 'bxdev.node',
            'icon': 'fa fa-leaf'
          }
        ]
      }
    ]
  }
]

在这里插入图片描述

代码:

const dataChild1 = data.map((x) => ({
  'text': x.name,
  'icon': 'fa fa-tree',
  'state': {
    'opened': false
  },
  'children': (x.type.map(x => ({
    'icon': 'fa fa-leaf',
    'text': x.name
  })))
}))
const result = dataNodeRoot.map(x => ({
  'text': 'root',
  'icon': 'fa fa-tree',
  'state': {
    'opened': true,
    'selected': true
  },
  children: dataChild1
}))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值