js 数组对象根据条件转换成多维数组

发布于 2022-09-12 01:04:55 字数 2793 浏览 22 评论 0

原数组
const NAVS = [
{

label: 'a',
path: 'a', 
children: [], 
code: 'HOME', 
levelCode: '00'

},
{

label: 'b',
path: '',
code: 'QYBZK',
levelCode: '01',
children: [
  {
    label: 'b1',
    code: 'QYBZKM',
    parentCode: 'QYBZK',
    levelCode: '0101',
    children: [
      {
        label: 'b11',
        code: 'ZYCBKM',
        parentCode: 'QYBZKM',
        levelCode: '010101',
        children: []
      }
    ]
  }
]

},
{

label: 'c',
path: '',
code: 'XMGL',
levelCode: '02',
children: [
  {
    label: 'c1',
    code: 'XMGK',
    parentCode: 'XMGL',
    levelCode: '0201',
    children: []
  },
  {
    label: 'c2',
    code: 'XMPZ',
    parentCode: 'XMGL',
    levelCode: '0202',
    children: []
  }
]

},
{

label: 'd',
code: 'CBDSJ',
levelCode: '03',
children: [
  {
    label: 'd1',
    code: 'CBZB',
    parentCode: 'CBDSJ',
    levelCode: '0301',
    children: [
      {
        label: 'd11',
        code: 'CBZBCX',
        parentCode: 'CBZB',
        levelCode: '030101',
        children: []
      },
      {
        label: 'd12',
        code: 'CBZBMXCX',
        parentCode: 'CBZB',
        levelCode: '030102',
        children: []
      }
    ]
  },
  {
    label: 'd2',
    code: 'ZBRK',
    parentCode: 'CBDSJ',
    levelCode: '0302',
    children: []
  }
]

}
];
筛选条件:
const obj = {

ZBRK: true,
CBZBMXCX: true,
XMPZ: true,
ZYCBKM: true,
HOME: true

}
想得到的数组:
const NAVS = [
{

label: 'a',
path: 'a', 
children: [], 
code: 'HOME', 
levelCode: '00'

},
{

label: 'b',
path: '',
code: 'QYBZK',
levelCode: '01',
children: [
  {
    label: 'b1',
    code: 'QYBZKM',
    parentCode: 'QYBZK',
    levelCode: '0101',
    children: [
      {
        label: 'b11',
        code: 'ZYCBKM',
        parentCode: 'QYBZKM',
        levelCode: '010101',
        children: []
      }
    ]
  }
]

},
{

label: 'c',
path: '',
code: 'XMGL',
levelCode: '02',
children: [
  {
    label: 'c2',
    code: 'XMPZ',
    parentCode: 'XMGL',
    levelCode: '0202',
    children: []
  }
]

},
{

label: 'd',
code: 'CBDSJ',
levelCode: '03',
children: [
  {
    label: 'd1',
    code: 'CBZB',
    parentCode: 'CBDSJ',
    levelCode: '0301',
    children: [
      {
        label: 'd12',
        code: 'CBZBMXCX',
        parentCode: 'CBZB',
        levelCode: '030102',
        children: []
      }
    ]
  },
  {
    label: 'd2',
    code: 'ZBRK',
    parentCode: 'CBDSJ',
    levelCode: '0302',
    children: []
  }
]

}
];

根据筛选条件的code值 去找数组中对应的曾经关系,过滤掉没有的。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2022-09-19 01:04:55

...前面刚回答过一个惊人相似的问题。写了个通用函数,直接用吧:

function treeFilter(func, tree = []) {
  return tree.filter(data => {
    data.children = treeFilter(func, data.children)
    return func(data) || data.children.length
  })
}

调用:

let result = treeFilter(data => obj[data.code], NAVS)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文