删除JSON数据中的指定项

本文介绍了一种方法来清理jstree插件生成的JSON数据,通过删除不必要的属性如'li_attr'、'state'等,使得JSON数据更加精简。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 这是为了删除jstree中的不需要的数据内容

                                 'data': [
                                        {
                                            "id": "abc",
                                            "text": "概况",
                                            "data": { "key": "key1", "Value": "value" },
                                            "state": { "opened": true },
                                            "children": [
                                                {
                                                    "text": "看板",
                                                    "state": { "selected": true },
                                                    "icon": "glyphicon glyphicon-flash"
                                                },
                                                { "text": "运维中心", "state": {} },
                                                { "text": "Child node 2", "state": { "disabled": false } },
                                            ]
                                        },
                                        {
                                            "text": "电力监控",
                                            "state": { "opened": true },
                                            "children": [
                                                {
                                                    "text": "电力数据",
                                                    "state": { "selected": true },
                                                    "icon": "glyphicon glyphicon-flash"
                                                },
                                                { "text": "电力数据报表", "state": { "disabled": true } },
                                                { "text": "配电图", "state": { "disabled": false } },
                                                { "text": "变压器监测", "state": { "disabled": false } },
                                            ]
                                        }
                                    ]

 删除json数据项的代码


    function test() {
        var data = $('#containertree').jstree(true).get_json();
 
        data.map((item) => {
            delete item["li_attr"];
            delete item["state"];
            delete item["a_attr"];
            delete item["icon"];

            //console.log("children1")
            item["children"].map((itemsub) => {
                delete itemsub["li_attr"];
                delete itemsub["state"];
                delete itemsub["a_attr"];
                delete itemsub["icon"];
            })
        });

        var mytext = JSON.stringify(data);
        alert(mytext);
    }

 删除项目后的数据内容

[
  {
    "id": "abc",
    "text": "概况",
    "data": {
      "key": "key1",
      "Value": "value"
    },
    "children": [
      {
        "id": "j1_2",
        "text": "看板",
        "data": {},
        "children": []
      },
      {
        "id": "j1_3",
        "text": "运维中心",
        "data": {},
        "children": []
      },
      {
        "id": "j1_4",
        "text": "Child node 2",
        "data": {},
        "children": []
      }
    ]
  },
  {
    "id": "j1_5",
    "text": "电力监控",
    "data": {},
    "children": [
      {
        "id": "j1_6",
        "text": "电力数据",
        "data": {},
        "children": []
      },
      {
        "id": "j1_7",
        "text": "电力数据报表",
        "data": {},
        "children": []
      },
      {
        "id": "j1_8",
        "text": "配电图",
        "data": {},
        "children": []
      },
      {
        "id": "j1_9",
        "text": "变压器监测",
        "data": {},
        "children": []
      }
    ]
  }
]

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值