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

Javascript 如何快速的对 Array 进行整合

  •  
  •   coolair · 2017-09-13 18:54:50 +08:00 · 895 次点击
    这是一个创建于 2623 天前的主题,其中的信息可能已经有所发展或是发生改变。
    var test_list = [
        {id: "1", name: "aaa", upper: "None"},
        {id: "2", name: "bbb", upper: "None"},
        {id: "3", name: "ccc", upper: "None"},
        {id: "4", name: "ddd", upper: "None"},
        {id: "5", name: "aaaa", upper: "1"},
        {id: "6", name: "aaaaa", upper: "5"},
        {id: "7", name: "bbbb", upper: "2"}
    ]
    
    如何快速的生成如下格式:
    var result_list = [
        {
            id: "1",
            name: "aaa",
            upper: "None",
            children: [
                {
                    id: "5",
                    name: "aaaa",
                    upper: "1",
                    children: [
                        {id: "6", name: "aaaaa", upper: "5"}
                    ]
                },
            ]
        }, {
            id: "2",
            name: "bbb",
            upper: "None",
            children: [
                {id: "7", name: "bbbb", upper: "2"}
            ]
        }, {
            id: "3",
            name: "ccc",
            upper: "None"
        }, {
            id: "4",
            name: "ddd",
            upper: "None"
        },
    ]
    
    2 条回复    2017-09-13 19:15:52 +08:00
    jtsai
        1
    jtsai  
       2017-09-13 18:59:19 +08:00 via Android
    不太记得 看看 JSON.stringify
    可以不
    yangff
        2
    yangff  
       2017-09-13 19:15:52 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3147 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:34 · PVG 21:34 · LAX 05:34 · JFK 08:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.