大家帮我看看,这个JS怎么处理分组?

作者站长头像
站长
· 阅读数 3

我有一个数组,如下:let datalist = [

"man-steady",
"woman-steady",
"woman-gentle",
"man-young",
"tcloud_10510000",
"tcloud_1001",
"tcloud_1002",
"tcloud_1018",
"tcloud_101015",
"tcloud_101050",
"tcloud_1004",
"tcloud_1050",
"tcloud_101056",
"tcloud_1003",
"tcloud_301041",
"WYS_15923375458_001",
"WYS_15332681888_001",
"WYS_15712098340_001",
"WYS_15532485330_001",
"WYS_15875942524_001",
"WYS_15923375458_002",
"WYS_15560857009_001",
"WYS_15332681888_002",
"DDS_15570649499_001",
"WYS_15875942524_002"

]

我想把这个数组处理成 这样的格式:let datalist2 = [

            {
                name: 'man',
                children: [{
                    name: 'man-steady',
                }, {
                    name: 'man-young',
                }]
            },
            {
                name: 'woman',
                children: [{
                    name: 'woman-steady',
                }, {
                    name: 'woman-gentle',
                }]
            },
            {
                name: 'WYS',
                children: [{
                    name: '15923375458',
                    children: [{
                        name: 'WYS_15923375458_001'
                    }]
                }, {
                    name: '15332681888',
                    children: [{
                        name: 'WYS_15332681888_001'
                    }, {
                        name: 'WYS_15332681888_002'
                    }]
                }]
            },
            {
                name: 'tcloud',
                children: [{
                    name: 'tcloud_10510000',

                }, {
                    name: 'tcloud_1001',

                }, {
                    name: 'tcloud_1002',
                }]
            }
        ]

类似于datalist2这样的数据结构,怎么才能实现呢?

回复
1个回答
avatar
test
2024-06-25
let datalist = [
  "man-steady",
  "woman-steady",
  "woman-gentle",
  "man-young",
  "tcloud_10510000",
  "tcloud_1001",
  "tcloud_1002",
  "tcloud_1018",
  "tcloud_101015",
  "tcloud_101050",
  "tcloud_1004",
  "tcloud_1050",
  "tcloud_101056",
  "tcloud_1003",
  "tcloud_301041",
  "WYS_15923375458_001",
  "WYS_15332681888_001",
  "WYS_15712098340_001",
  "WYS_15532485330_001",
  "WYS_15875942524_001",
  "WYS_15923375458_002",
  "WYS_15560857009_001",
  "WYS_15332681888_002",
  "DDS_15570649499_001",
  "WYS_15875942524_002",
]

type Data = { name: string; children?: Data[] }

function parse(
  arr: Data[],
  [name, splitter, ...rest]: string[],
  nameAll = "",
) {
  if (!arr.some((v) => v.name === name)) arr.push({ name })
  const d = arr.find((v) => v.name === name)!
  if (rest.length) {
    d.children ??= []
    parse(d.children, rest, `${nameAll}${name}${splitter}`)
  } else d.name = nameAll + d.name
  return arr
}

const p = datalist.map((d) => d.split(/(-|_)/))
  .reduce<Data[]>((p, c) => parse(p, c), [])

console.log(JSON.stringify(p))

结果:

[
  {
    "name": "man",
    "children": [{ "name": "man-steady" }, { "name": "man-young" }]
  },
  {
    "name": "woman",
    "children": [{ "name": "woman-steady" }, { "name": "woman-gentle" }]
  },
  {
    "name": "tcloud",
    "children": [
      { "name": "tcloud_10510000" },
      { "name": "tcloud_1001" },
      { "name": "tcloud_1002" },
      { "name": "tcloud_1018" },
      { "name": "tcloud_101015" },
      { "name": "tcloud_101050" },
      { "name": "tcloud_1004" },
      { "name": "tcloud_1050" },
      { "name": "tcloud_101056" },
      { "name": "tcloud_1003" },
      { "name": "tcloud_301041" }
    ]
  },
  {
    "name": "WYS",
    "children": [
      {
        "name": "15923375458",
        "children": [
          { "name": "WYS_15923375458_001" },
          { "name": "WYS_15923375458_002" }
        ]
      },
      {
        "name": "15332681888",
        "children": [
          { "name": "WYS_15332681888_001" },
          { "name": "WYS_15332681888_002" }
        ]
      },
      {
        "name": "15712098340",
        "children": [{ "name": "WYS_15712098340_001" }]
      },
      {
        "name": "15532485330",
        "children": [{ "name": "WYS_15532485330_001" }]
      },
      {
        "name": "15875942524",
        "children": [
          { "name": "WYS_15875942524_001" },
          { "name": "WYS_15875942524_002" }
        ]
      },
      { "name": "15560857009", "children": [{ "name": "WYS_15560857009_001" }] }
    ]
  },
  {
    "name": "DDS",
    "children": [
      { "name": "15570649499", "children": [{ "name": "DDS_15570649499_001" }] }
    ]
  }
]
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容