5 Star 62 Fork 13

阿森 / js.tree

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
exclude.md 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
阿森 提交于 2023-11-16 19:34 . docs: 修复文档拼写检查错误

排除数据

可以使用 exclude 函数过滤掉不想要的数据

filter 方法的区别:

  • 回调函数返回 true 的数据将被过滤
  • 如果子级都被过滤掉了,那父级也会被排除

回调函数参数:

  • node - 当前节点对象
  • index - 在同级中的索引
  • parents - 所有上级的节点对象
import { exclude } from '@zhengxs/js.tree'

const data = [
  {
    title: '财务',
    children: [{ title: '收入流失' }, { title: '财务设置' }],
  },
  {
    title: '站点设置',
    children: [{ title: '菜单维护' }, { title: '角色维护' }],
  },
]

const result = exclude(data, (node, index, parents) => {
  return node.title.includes('财务') || node.title.includes('角色')
})
// ->
// [
//   {
//     title: '站点设置',
//     children: [{ title: '菜单维护' }],
//   },
// ]

默认子级列表的属性名称是 children,可以通过第三个参数修改

import { exclude } from '@zhengxs/js.tree'

const data = [
  {
    title: '财务',
    children: [{ title: '收入流失' }, { title: '财务设置' }],
  },
  {
    title: '站点设置',
    children: [{ title: '菜单维护' }, { title: '角色维护' }],
  },
]

// 如果不是 children 属性,可以通过第三个参数指定,可选
const result = exclude(
  data,
  ({ title }) => title.includes('财务') || title.includes('角色'),
  'items'
)
// ->
// [
//   {
//     title: '站点设置',
//     items: [{ title: '菜单维护' }],
//   },
// ]
TypeScript
1
https://gitee.com/zhengxs2018/js.tree.git
git@gitee.com:zhengxs2018/js.tree.git
zhengxs2018
js.tree
js.tree
main

搜索帮助