我有一个数组,如下:
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" }] } ] }]
这个问题可以通过 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
结构相同的数组。当然,这只是一个基础的实现,可能还需要根据具体的需求进行一些调整。
点击优惠券弹出选择优惠券的列表,选中之后,已经修改了 this.data.shopList[idx2].coupons_price= cou_money 并且已经打印了cou_money 为当前选择的优惠券金额 而且也this.setData了 但是选择之后,隐藏弹窗 优惠券处却还是空, 但是当我再点击弹窗的时候,值去出现了,不知道为什么会有这种页面视图刷新不及时的问题?
下面是监控网卡流量的 shell 脚本,运行后提示: expr:语法错误 看来看去不知道哪里出错了。
演示Demo 图中 一个flex容器内部有四个div元素, div内部的img的宽度设置为100px。 默认情况下flex-wrap=nowrap flex子项会一行显示且不允许换行,因此可能会出现宽度溢出的问题。 问题1 “因此可能会出现宽度溢出的问题” : 这个宽度溢出应该也是有 条件的吧? 虽然flex子项的累计宽度大于flex容器,但是默认情况下 flex子项是允许收缩的。 如果收缩以后还
问题内容: 有人可以发现我的错误吗,这应该是SQL中的合法查询吗? ON子句中的未知列u.usr_auto_key 这是数据库模式: 这是查询… 问题答案: 尝试切换,并在from子句中:
处理成 [11,1201,1304,1305]