Dreamlanga 2022-12-28 04:49 采纳率: 25%
浏览 173
已结题

js做一个数组筛选。

有这样一个数组,如果他的子元素里的disabled是false 就把他筛选出来,并且把他的最上级的元素也保留,把disabeld不是false的过滤掉

  const arr = [
    {
      key: '17',
      value: '17',
      label: 'Data Arch',
      disabled: true,
      children: [
        { key: '17', value: '17', label: 'Data Arch', disabled: true },
        {
          key: '8',
          value: '8',
          label: '留',
          disabled: false,
          children: [{ key: '9', value: '9', label: '留', disabled: false }],
        },
      ],
    },
    { key: '2160', value: '2160', label: 'Data Platform', disabled: true },
    { key: '25', value: '25', label: 'Enterprise Application', disabled: true },
    { key: '2161', value: '2161', label: 'Infrastructure', disabled: true },
    { key: '24', value: '24', label: 'Lark', disabled: true },
  ];

最后希望得到这样一个筛选结果🙏

  const filters = [
    {
      key: '17',
      value: '17',
      label: 'Data Arch',
      disabled: true,
      children: [
        {
          key: '8',
          value: '8',
          label: '留',
          disabled: false,
          children: [{ key: '9', value: '9', label: '留', disabled: false }],
        },
      ],
    }
  ];

  • 写回答

10条回答 默认 最新

  • 游一游走一走 2022-12-28 11:12
    关注
    const arr = [
        {
            key: '17',
            value: '17',
            label: 'Data Arch',
            disabled: true,
            children: [
                {key: '17', value: '17', label: 'Data Arch', disabled: true},
                {
                    key: '8',
                    value: '8',
                    label: '留',
                    disabled: false,
                    children: [{key: '9', value: '9', label: '留', disabled: false}],
                },
            ],
        },
        {key: '2160', value: '2160', label: 'Data Platform', disabled: true},
        {key: '25', value: '25', label: 'Enterprise Application', disabled: true},
        {key: '2161', value: '2161', label: 'Infrastructure', disabled: true},
        {key: '24', value: '24', label: 'Lark', disabled: true},
    ];
    
    function filterArr(arr) {
        function filterData(node) {
            if (!node.children) {
                return node.disabled ? null : node;
            } else {
                let children = node.children.map(filterData).filter(item => item);
                if (children.length > 0) {
                    return {...node, children}
                } else if (!node.disabled) {
                    delete node.children
                    return node
                }
                return null
            }
        }
    
        return arr.map(filterData).filter(item => item)
    }
    
    console.log(JSON.stringify(filterArr(arr)))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月29日
  • 已采纳回答 12月28日
  • 修改了问题 12月28日
  • 创建了问题 12月28日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测