V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
newmoyupoi
V2EX  ›  问与答

代码养蛊,求个递归算法

  •  
  •   newmoyupoi · 2019-08-08 17:06:08 +08:00 · 1988 次点击
    这是一个创建于 1694 天前的主题,其中的信息可能已经有所发展或是发生改变。

    求个数据结果解析递归算法,不要求很复杂 数据结构如下: data = [{ value: 'Root', defaultValue: '根节点', key: '0-1', parentKey: '0', isEditable: false, title: 'xxx' children: [{ defaultValue: '子树', isEditable: false, key: '0-10.4061361533484542', parentKey: '0-1', value: '111', title: 'xxx', children: [{ defaultValue: '子子树', isEditable: false, key: '0-10-1.92052037502357025', parentKey: '0-1', value: '111', title: 'xxx' } }, { defaultValue: '子树', isEditable: false, key: '0-10.6243359336024665', parentKey: '0-1', value: '222', title: 'xxx' }] }] 根据上面的数据结构,想把所有级别里面的 title 删掉,保证数据结构不变,只 delete 整个 title,求大神给个好的处理办法

    15 条回复    2019-08-09 19:47:06 +08:00
    newmoyupoi
        1
    newmoyupoi  
    OP
       2019-08-08 17:09:02 +08:00
    忘记说了,现在用的框架 react,如果有好的解法可以支付现金作为回报
    jadehare
        2
    jadehare  
       2019-08-08 17:24:26 +08:00   ❤️ 1
    转成 json 找字段 title 删掉再转回来
    newmoyupoi
        3
    newmoyupoi  
    OP
       2019-08-08 17:26:24 +08:00
    @jadehare #2 牛 x !!!
    newmoyupoi
        4
    newmoyupoi  
    OP
       2019-08-08 17:33:12 +08:00
    @jadehare #2 如果 title 里面有 function 转不成 json 字符串呢?
    octree
        5
    octree  
       2019-08-08 17:37:57 +08:00   ❤️ 1
    大兄弟你这个有语法错误。。。。
    我感觉这样应该可以。


    function mapValues(obj, transform) {
    let result = {}
    for (let key in obj) {
    result[key] = transform(obj[key])
    }
    return result
    }

    function removeTitleRecursively(any) {
    if (Array.isArray(any)) {
    return any.map(removeTitleRecursively)
    } else if (typeof any === 'object') {
    delete any.title
    return mapValues(any, removeTitleRecursively)
    } else {
    return any
    }
    }
    newmoyupoi
        6
    newmoyupoi  
    OP
       2019-08-08 17:42:16 +08:00
    data = [{
    value: 'Root',
    defaultValue: '根节点',
    key: '0-1',
    parentKey: '0',
    isEditable: false,
    title: 'xxx',
    children: [{
    defaultValue: '子树',
    isEditable: false,
    key: '0-10.4061361533484542',
    parentKey: '0-1',
    value: '111',
    title: 'xxx',
    children: [{
    defaultValue: '子子树',
    isEditable: false,
    key: '0-10-1.92052037502357025',
    parentKey: '0-1',
    value: '111',
    title: 'xxx'
    }]
    }, {
    defaultValue: '子树',
    isEditable: false,
    key: '0-10.6243359336024665',
    parentKey: '0-1',
    value: '222',
    title: 'xxx'
    }]
    }]


    修改了下,这个语法没错误
    octree
        7
    octree  
       2019-08-08 17:51:21 +08:00   ❤️ 1
    @newmoyupoi 上面代码应该发给你了
    newmoyupoi
        8
    newmoyupoi  
    OP
       2019-08-08 18:07:28 +08:00
    @octree #7 调用 removeTitleRecursively(数据)不是很好使,any.map(removeTitleRecursively)这个里面的 removeTitleRecursively 在 react 里面改成 this.removeTitleRecursively?
    octree
        9
    octree  
       2019-08-08 18:11:18 +08:00   ❤️ 1
    @newmoyupoi 这两个函数放 React Component
    外面就行了吧。
    newmoyupoi
        10
    newmoyupoi  
    OP
       2019-08-08 18:11:31 +08:00
    @octree #7 可以了 搞定 感谢 ing~
    xtray
        11
    xtray  
       2019-08-08 20:25:13 +08:00 via Android
    直接用正则或 sed 删不就行了
    crs0910
        12
    crs0910  
       2019-08-08 22:12:59 +08:00   ❤️ 1
    const removeTitle = data => JSON.parse(JSON.stringify(data), (k, v) => k === 'title' ? undefined : v)
    newmoyupoi
        13
    newmoyupoi  
    OP
       2019-08-09 19:33:17 +08:00
    @crs0910 #12 不行,去掉 title 的目的不是为了去掉这个字段,而是 title 里面是个 function,无法用 JSON.stringify 做转换,所以 JSON.stringify(data)这一步是不行的
    crs0910
        14
    crs0910  
       2019-08-09 19:39:14 +08:00
    @newmoyupoi #13 不明白你的意思。哈哈
    newmoyupoi
        15
    newmoyupoi  
    OP
       2019-08-09 19:47:06 +08:00
    @crs0910 #14 ![]( )
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5392 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 07:05 · PVG 15:05 · LAX 00:05 · JFK 03:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.