如图,想取出此树形结构的所有selected为true的属性 怎么取?

取出来后仍为树形,只不过将所有selected为false的删除了。

[
  {
    "createBy": "123123",
    "createTime": "2023-02-09 13:29:52",
    "updateBy": null,
    "updateTime": null,
    "delBy": null,
    "delTime": null,
    "del": 1,
    "id": 464,
    "name": "��Уָ�����5",
    "code": "c6580aad-314a-40bc-a27b-a99d7af2975c",
    "type": 1,
    "level": 1,
    "children": null,
    "fcode": "",
    "selected": false
  },
  {
    "createBy": "123123",
    "createTime": "2023-02-09 13:29:47",
    "updateBy": null,
    "updateTime": null,
    "delBy": null,
    "delTime": null,
    "del": 1,
    "id": 463,
    "name": "��Уָ�����4",
    "code": "3c714d9f-38a9-4726-aaf8-c6a1f136722c",
    "type": 1,
    "level": 1,
    "children": [
      {
        "createBy": "123123",
        "createTime": "2023-02-09 13:34:36",
        "updateBy": null,
        "updateTime": null,
        "delBy": null,
        "delTime": null,
        "del": 1,
        "id": 475,
        "name": "��Уָ�����4-1",
        "code": "5f368cac-6c40-48df-98bc-89e24de5721d",
        "type": 1,
        "level": 2,
        "children": [
          {
            "createBy": "fc28e1bc-096d-463f-b734-30b46470fc2d",
            "createTime": "2023-02-09 14:53:19",
            "updateBy": null,
            "updateTime": null,
            "delBy": null,
            "delTime": null,
            "del": 1,
            "id": 497,
            "name": "��Уָ�����4-1-2",
            "code": "cd4aaa11-fbaf-4843-bee4-123cfd1b1211",
            "type": 1,
            "level": 3,
            "children": null,
            "fcode": "5f368cac-6c40-48df-98bc-89e24de5721d",
            "selected": false
          },
          {
            "createBy": "123123",
            "createTime": "2023-02-09 13:34:45",
            "updateBy": null,
            "updateTime": null,
            "delBy": null,
            "delTime": null,
            "del": 1,
            "id": 476,
            "name": "��Уָ�����4-1-1",
            "code": "500a5375-8b05-4088-ad57-5b69585ab796",
            "type": 1,
            "level": 3,
            "children": null,
            "fcode": "5f368cac-6c40-48df-98bc-89e24de5721d",
            "selected": false
          }
        ],
        "fcode": "3c714d9f-38a9-4726-aaf8-c6a1f136722c",
        "selected": false
      }
    ],
    "fcode": "",
    "selected": false
  },
  {
    "createBy": "123123",
    "createTime": "2023-02-09 13:29:41",
    "updateBy": null,
    "updateTime": null,
    "delBy": null,
    "delTime": null,
    "del": 1,
    "id": 462,
    "name": "��Уָ�����3",
    "code": "3c00211a-7968-47d4-ba6b-b2ae348e5676",
    "type": 1,
    "level": 1,
    "children": [
      {
        "createBy": "168b632e-4b59-41c5-93bc-7080e4ead5c8",
        "createTime": "2023-02-09 13:47:29",
        "updateBy": null,
        "updateTime": null,
        "delBy": null,
        "delTime": null,
        "del": 1,
        "id": 487,
        "name": "����",
        "code": "979dd14c-d002-498c-91ee-d3be46ab1c80",
        "type": 1,
        "level": 2,
        "children": null,
        "fcode": "3c00211a-7968-47d4-ba6b-b2ae348e5676",
        "selected": false
      },
      {
        "createBy": "123123",
        "createTime": "2023-02-09 13:34:15",
        "updateBy": null,
        "updateTime": null,
        "delBy": null,
        "delTime": null,
        "del": 1,
        "id": 473,
        "name": "��Уָ�����3-1",
        "code": "3942bffd-2f04-4c67-887d-7e5c627ea45e",
        "type": 1,
        "level": 2,
        "children": [
          {
            "createBy": "123123",
            "createTime": "2023-02-09 13:34:23",
            "updateBy": null,
            "updateTime": null,
            "delBy": null,
            "delTime": null,
            "del": 1,
            "id": 474,
            "name": "��Уָ�����3-1-1",
            "code": "83c934b5-bbe9-4eb9-9da7-1035d74e154d",
            "type": 1,
            "level": 3,
            "children": null,
            "fcode": "3942bffd-2f04-4c67-887d-7e5c627ea45e",
            "selected": false
          }
        ],
        "fcode": "3c00211a-7968-47d4-ba6b-b2ae348e5676",
        "selected": false
      }
    ],
    "fcode": "",
    "selected": false
  },
  {
    "createBy": "123123",
    "createTime": "2023-02-09 13:29:32",
    "updateBy": null,
    "updateTime": null,
    "delBy": null,
    "delTime": null,
    "del": 1,
    "id": 461,
    "name": "��Уָ�����2",
    "code": "863b4a2f-9cd3-4825-bbd8-390a67d5a350",
    "type": 1,
    "level": 1,
    "children": [
      {
        "createBy": "123123",
        "createTime": "2023-02-09 13:33:48",
        "updateBy": null,
        "updateTime": null,
        "delBy": null,
        "delTime": null,
        "del": 1,
        "id": 471,
        "name": "��Уָ�����2-1",
        "code": "53cef9f4-f997-4174-915d-340056a5ac23",
        "type": 1,
        "level": 2,
        "children": [
          {
            "createBy": "123123",
            "createTime": "2023-02-09 13:33:56",
            "updateBy": null,
            "updateTime": null,
            "delBy": null,
            "delTime": null,
            "del": 1,
            "id": 472,
            "name": "��Уָ�����2-1-2",
            "code": "b137a77c-9f46-44b4-b0bc-9a904323c710",
            "type": 1,
            "level": 3,
            "children": null,
            "fcode": "53cef9f4-f997-4174-915d-340056a5ac23",
            "selected": false
          }
        ],
        "fcode": "863b4a2f-9cd3-4825-bbd8-390a67d5a350",
        "selected": false
      }
    ],
    "fcode": "",
    "selected": false
  },
  {
    "createBy": "123123",
    "createTime": "2023-02-09 13:29:24",
    "updateBy": null,
    "updateTime": null,
    "delBy": null,
    "delTime": null,
    "del": 1,
    "id": 460,
    "name": "��Уָ�����1",
    "code": "42f7f7cc-4875-4822-b3cd-707e82f28478",
    "type": 1,
    "level": 1,
    "children": [
      {
        "createBy": "123123",
        "createTime": "2023-02-09 13:30:58",
        "updateBy": "123123",
        "updateTime": "2023-02-09T05:31:23.000+00:00",
        "delBy": null,
        "delTime": null,
        "del": 1,
        "id": 466,
        "name": "��Уָ�����1-2",
        "code": "9df431b8-2bb0-4a50-a8a5-ef1a4fa497be",
        "type": 1,
        "level": 2,
        "children": [
          {
            "createBy": "123123",
            "createTime": "2023-02-09 13:32:29",
            "updateBy": null,
            "updateTime": null,
            "delBy": null,
            "delTime": null,
            "del": 1,
            "id": 470,
            "name": "��Уָ�����1-2-2",
            "code": "75c2e1bd-ab5c-4c83-9fe7-e8029066a55f",
            "type": 1,
            "level": 3,
            "children": null,
            "fcode": "9df431b8-2bb0-4a50-a8a5-ef1a4fa497be",
            "selected": true
          },
          {
            "createBy": "123123",
            "createTime": "2023-02-09 13:32:06",
            "updateBy": null,
            "updateTime": null,
            "delBy": null,
            "delTime": null,
            "del": 1,
            "id": 469,
            "name": "��Уָ�����1-2-1",
            "code": "00611e5f-d46e-4278-8f37-a9cab8ea0576",
            "type": 1,
            "level": 3,
            "children": null,
            "fcode": "9df431b8-2bb0-4a50-a8a5-ef1a4fa497be",
            "selected": true
          }
        ],
        "fcode": "42f7f7cc-4875-4822-b3cd-707e82f28478",
        "selected": true
      },
      {
        "createBy": "123123",
        "createTime": "2023-02-09 13:30:45",
        "updateBy": "123123",
        "updateTime": "2023-02-09T05:31:17.000+00:00",
        "delBy": null,
        "delTime": null,
        "del": 1,
        "id": 465,
        "name": "��Уָ�����1-1",
        "code": "d487d6be-8a84-4906-a0ad-f2eba88b0c21",
        "type": 1,
        "level": 2,
        "children": [
          {
            "createBy": "123123",
            "createTime": "2023-02-09 13:31:48",
            "updateBy": null,
            "updateTime": null,
            "delBy": null,
            "delTime": null,
            "del": 1,
            "id": 468,
            "name": "��Уָ�����1-1-2",
            "code": "61a4adec-463c-4fef-9eb8-4cbef5fd37b0",
            "type": 1,
            "level": 3,
            "children": null,
            "fcode": "d487d6be-8a84-4906-a0ad-f2eba88b0c21",
            "selected": false
          },
          {
            "createBy": "123123",
            "createTime": "2023-02-09 13:31:38",
            "updateBy": null,
            "updateTime": null,
            "delBy": null,
            "delTime": null,
            "del": 1,
            "id": 467,
            "name": "��Уָ�����1-1-1",
            "code": "759f31e3-ea16-4246-a385-21baf46245ea",
            "type": 1,
            "level": 3,
            "children": null,
            "fcode": "d487d6be-8a84-4906-a0ad-f2eba88b0c21",
            "selected": false
          }
        ],
        "fcode": "42f7f7cc-4875-4822-b3cd-707e82f28478",
        "selected": false
      }
    ],
    "fcode": "",
    "selected": true
  }
]
阅读 1.6k
5 个回答
function filter(children){
  if(children){
    const filtered=children.filter((item)=>{
      if(item.selected){
        if(item.children){
          item.children=filter(item.children)
        }
        return item
      }
    })
    return filtered
  }
}



const result=filter(arr)

console.log(result)

function filterData(arr) {
    return arr.filter(item => item.selected).map(item => ({
        ...item,
        children: item.children &&  filterData(item.children)
    }))
}

var a = [

   { fcode:111,
     selected: false},
         { fcode:222,
            selected: false},
            { fcode:333,
                children:[{fcode:666,
                    selected: true}],
                selected: false},
                { fcode:444,
                    selected: true}

]

const onTreeKey = (target, init = []) => {
target.forEach(item => {

  item.selected  && init.push(item);    
  item.children && onTreeKey(item.children, init);

});
return init;
};
var c = onTreeKey(a)
console.log(c)
// 0 {fcode: 666, selected: true}
// 1 {fcode: 444, selected: true}

let f = (nodes) => nodes?.filter(i => i.selected).map(i => {i.children = f(i.children); return i});
function getSelectedItems(data) {
  var pickup = [];
  if(data instanceof Array) {
    data.forEach(function(level1) {
      if(level1.selected){
        var copyLevel1 = Object.assign({children: null}, level1);
        copy.push(copyLevel1);
        if(level1.children && level1.children instanceof Array) {
          level1.children.forEach(function(level2){
            if(level2.selected && level2.children && level2.children instanceof Array) {
              copyLevel1.children = [];
              copyLevel1.children.push(Object.assign({children: null, level2}));
              level2.children.forEach(level3 => {
                if(level3.children && level3.children instanceof Array) {
                  copyLevel1.children[copyLevel1.children.length - 1] = 
                    level3.children.map(level3_item => {
                      return level3_item.selected;
                    })
                }
              })
            }
          })
        }
      }
    })
  }
  console.log(copy)
}

getSelectedItems(data)
推荐问题
宣传栏