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

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


回答:

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" }] }

]

}

]

以上是 大家帮我看看,这个JS怎么处理分组? 的全部内容, 来源链接: utcz.com/p/935093.html

回到顶部