Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter 函数存在问题 #5

Open
xiaomoumou opened this issue Feb 20, 2024 · 2 comments
Open

filter 函数存在问题 #5

xiaomoumou opened this issue Feb 20, 2024 · 2 comments

Comments

@xiaomoumou
Copy link

  filter (tree, func, config = {}) {
    config = getConfig(config)
    const { children } = config
    function listFilter (list) {
      return list.map(node => ({ ...node })).filter(node => {
        node[children] = node[children] && listFilter(node[children])
        return func(node) || (node[children] && node[children].length)
      })
    }
    return listFilter(tree)
  },

这里return func(node) || (node[children] && node[children].length)写法有些问题。

应该改为return func(node) || (func(node)&&node[children] && node[children].length)

@xiaomoumou
Copy link
Author

xiaomoumou commented Feb 20, 2024

可以试下这个demo,看下注释的那行

  const DEFAULT_CONFIG = {
    id: 'id',
    children: 'children',
    pid: 'pid'
  }

  const getConfig = config => Object.assign({}, DEFAULT_CONFIG, config)


  const fn = (obj, value, keyName = "id") => obj[keyName] !== value

  const fn1 = obj => fn(obj, 12)
  const data = [{
      id: 1,
      name: '一、John Brown sr.',
      age: 60,
      address: 'New York No. 1 Lake Park',
      children: [{
          id: 11,
          name: 'John Brown',
          age: 42,
          address: 'New York No. 2 Lake Park',
        },
        {
          id: 12,
          name: 'John Brown jr.',
          age: 30,
          address: 'New York No. 3 Lake Park',
          children: [{
            id: 121,
            name: 'Jimmy Brown',
            age: 16,
            address: 'New York No. 3 Lake Park',
          }, ],
        }
      ],
    }
  ]



  const toolFilter = (tree, func, config = {}) => {
    config = getConfig(config)
    const {
      children
    } = config

    function listFilter(list) {
      return list.map(node => ({
        ...node
      })).filter(node => {
        node[children] = node[children] && listFilter(node[children])
        // return func(node) || (node[children] && node[children].length)
        return func(node) || (func(node)&&node[children] && node[children].length)
      })
    }
    return listFilter(tree)
  }

  const result2 = toolFilter(data, fn1)

@wintc23
Copy link
Owner

wintc23 commented Feb 23, 2024

@xiaomoumou 你好,两种写法是不同的逻辑,主要是看预期是怎样的。
写法一:
return func(node) || (node[children] && node[children].length)
这样写的预期是:如果子节点满足筛选条件,则保留父节点。

写法二:
return func(node) || (func(node)&&node[children] && node[children].length)
这样写的预期是:只要父节点不满足条件,那其所有子节点都不保留(无论是否满足条件)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants