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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!