解析嵌套数组对象,并创建一个新数组

时间:2015-06-22 15:41:17

标签: javascript arrays kendo-treeview

我有一个嵌套的对象数组,我在JavaScript中解析,但是我在创建目标数组时遇到了困难。

这里是正确呈现的Kendo UI树视图,其中附有一个示例数组(即最终dataSource数组的外观):

   http://plnkr.co/edit/YpuXJyWgGI7h1bWR0g70?p=preview 

Kendo UI treeview widget

我的源数组已嵌套" children"数组,其中叶节点是" drms"阵列。

当我解析嵌套的孩子时,我正在尝试执行以下操作:

  1. 非空的孩子"数组需要重命名为" drms"
  2. 空的孩子"数组需要删除
  3. 以下是一个示例源数组:

    
    
    [	/* SOURCE ARRAY */
        {
            "category": "Market Risk",
            "sysid": 1,
            "children": [
                {
                    "category": "General",
                    "sysid": 2,
                    "children": [],
                    "drms": [
                        {
                            "name": "1 Day VaR (99%)"
                        },
                        {
                            "name": "10 Day VaR (99%)"
                        }
                    ]
                },
                {
                    "category": "Decomposition",
                    "sysid": 3,
                    "children": [],
                    "drms": [
                        {
                            "name": "1D VaR Credit"                     
                        },
                        {
                            "name": "1D VaR Equity"
                        }
                    ]
                },
                {
                    "category": "Sensitivities",
                    "sysid": 4,
                    "children": [
                        {
                            "category": "Currency Pairs",
                            "sysid": 11,
                            "children": [
                                {
                                    "category": "EUR/USD",
                                    "sysid": 12,
                                    "children": [],
                                    "drms": [
                                        {
                                            "name": "Spot"                                      
                                        },
                                        {
                                            "name": "Spot - 0.01"                                       
                                        }                                    
                                    ]
                                }
                            ],
                            "drms": []
                        }
                    ],
                    "drms": []
                }
            ],
            "drms": []
        },
        {
            "category": "CCR",
            "sysid": 6,
            "children": [
                {
                    "category": "General (CCR)",
                    "sysid": 7,
                    "children": [],
                    "drms": [
                        {
                            "name": "MTM"                       
                        },
                        {
                            "name": "PFE"                        
                        }
                    ]
                }
            ],
            "drms": []
        }    
    ]
    
    
    

    和我手动修改的目标数组,以呈现Kendo TreeView:

    
    
    [		
          {
              "category": "Market Risk",
              "sysid": 1,
              "drms": [
                  {
                      "category": "General",
                      "sysid": 2,
                      
                      "drms": [
                          {
                              "name": "1 Day VaR (99%)",
                              "riskMeasure": "-PERCENTILE(SUM([99_HSVaR]:[1D]),1)",
                              "cubeVector": "[99_HSVaR]:[1D]"
                          },
                          {
                              "name": "10 Day VaR (99%)",
                              "riskMeasure": "-PERCENTILE(SUM([99_HSVaR]:[2W]),1)",
                              "cubeVector": "[99_HSVaR]:[2W]"
                          },
                          {
                              "name": "Day over Day VaR",
                              "riskMeasure": "-PERCENTILE(SUM(today),1)+PERCENTILE(SUM(yesterday),1)",
                              "cubeVector": "[BASELINE]:[99_HSVaR]:[2W] as today, [BASELINE-1]:[99_HSVaR]:[2W] as yesterday"
                          }
                      ]
                  },
                  {
                      "category": "Decomposition",
                      "sysid": 3,
                      "drms": [
                          {
                              "name": "1D VaR Credit",
                              "riskMeasure": "SUM([99_HSVaR]:[1D CR])",
                              "cubeVector": "[99_HSVaR]:[1D CR]"
                          },
                          {
                              "name": "1D VaR Equity",
                              "riskMeasure": "SUM([99_HSVaR]:[1D EQ])",
                              "cubeVector": "[99_HSVaR]:[1D EQ]"
                          }
                      ]
                  },
                  {
                      "category": "Sensitivities",
                      "sysid": 4,
                      "drms": [
                          {
                              "category": "Currency Pairs",
                              "sysid": 11,
                              "drms": [
                                  {
                                      "category": "EUR/USD",
                                      "sysid": 12,
                                      "children": [],
                                      "drms": [
                                          {
                                              "name": "Spot",
                                              "riskMeasure": "SUM([EUR_USD by EUR]:[Spot - 0.00])",
                                              "cubeVector": "[EUR_USD by EUR]:[Spot - 0.00]"
                                          },
                                          {
                                              "name": "Spot - 0.01",
                                              "riskMeasure": "SUM([EUR_USD by EUR]:[Spot - 0.01])",
                                              "cubeVector": "[EUR_USD by EUR]:[Spot - 0.01]"
                                          }
                                      ]
                                  }
                              ],
                          }
                      ]
                  }
              ],
          },
          {
              "category": "CCR",
              "sysid": 6,
              "drms": [
                  {
                      "category": "General (CCR)",
                      "sysid": 7,
                      
                      "drms": [
                          {
                              "name": "MTM",
                              "riskMeasure": "SUM(MTM:MTM)",
                              "cubeVector": "MTM:MTM"
                          },
                          {
                              "name": "PFE",
                              "riskMeasure": "PERCENTILE(SUM(x),95)",
                              "cubeVector": "[Simulated]:[MCS] IF value > 0 as x"
                          }
                      ]
                  }
              ]
          }
        ]
    
    
    

    我的JavaScript例程尚未完成。解析嵌套子节点时有点混乱:

    
    
    function create_TempDrmTree() {
                // convert raw def risk measures (drm) data into a tree format for the Kendo treeview widget.
                var data = getTestDrmTree();
    
                var drmsJson = [];
                var i = 0;
                _.each(data, function (item) {
                    drmsJson.push({ "category": item.category, drms: [] });
                    if (item.children.length > 0) {
                        pushDrms(item.children);
                    }
                    i++;
                });
                
                function pushDrms(children) {
                    _.each(children, function (item) {
                        if (item.children.length > 0) {
                            pushDrms(item.children);
                        }
                        else {
                            // no more children, so get the DRMs from item
                            // leaving tempty children[] causes an issue on Kendo treeview
                            delete item.children;   
                            drmsJson[i]["drms"] = item;
                        }
                    });
                }
    
                return drmsJson;
            }
    
    
    

1 个答案:

答案 0 :(得分:1)

根据您最初的想法,我稍微修改了一下。这适用于我的场景。

编辑:从What is the most efficient way to clone an object?开始,我们可以轻松创建一个全新的阵列并保持原始阵列不受影响。

function parseDrmTree(items, isCloned) {

    if (isCloned !== true) {
        // Create json string from original item, and then parse it.
        // And only do this at the root.
        items = JSON.parse(JSON.stringify(items));
        isCloned = true;
    }
    // reparse the DRM source tree, renaming "children" array to "drms".
    items.forEach(function (item, index) {
        if (item.children.length > 0) {
            // copy children[] to  drms[]
            item.drms = parseDrmTree(item.children, isCloned);
        }                
        // children[] is empty; drms[] exist at this level
        delete item.children;
    }, this);
    return items;
}

您不必提供isCloned值,只需输入目标数组,该函数将创建一个全新的数组,并使用它来创建所需的结构,并且原点不受影响。