当前位置: 首页 > 知识库问答 >
问题:

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

鲁龙野
2023-11-01

我有一个数组,如下:
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这样的数据结构,怎么才能实现呢?

共有2个答案

郏瀚
2023-11-01
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" }] }    ]  }]
仲孙德惠
2023-11-01

这个问题可以通过 JavaScript 中的一些基础算法方法解决,比如 map、filter 和 reduce。以下是一个可能的解决方案:

首先,我们需要创建一个空的结果数组 datalist2 和一个空对象 groups。然后,我们遍历 datalist 数组,对每一个元素,我们检查它的前缀(即第一个单词)是否已经在 groups 对象中存在。如果存在,我们就将其添加到相应的子数组中;如果不存在,我们就在 groups 对象中创建一个新的条目,并将这个元素添加到子数组中。

以下是具体的代码实现:

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 = [];let groups = {};for (let i = 0; i < datalist.length; i++) {    let item = datalist[i];    let mainPart = item.split('-')[0];  // 这将取得类似 'man', 'woman', 'tcloud', 'WYS' 等部分    let childPart = item.split('-')[1]; // 这将取得类似 'steady', 'gentle', 'young' 等部分    if (!groups[mainPart]) {        groups[mainPart] = {name: mainPart, children: []};        datalist2.push(groups[mainPart]);    }    let group = groups[mainPart];    group.children.push({name: childPart, children: []});}

这段代码将生成一个与 datalist2 结构相同的数组。当然,这只是一个基础的实现,可能还需要根据具体的需求进行一些调整。

 类似资料: