gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:47:13 26 4
gpt4 key购买 nike

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

这是正确渲染的 Kendo UI Treeview ,其中附加了一个示例数组(即最终数据源数组的样子):

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

Kendo UI treeview widget

我的源数组具有嵌套的“子”数组,其中叶节点是“drms”数组。

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

  1. 非空“children”数组需要重命名为“drms”
  2. 需要删除空的“children”数组

这是一个示例源数组:

[	/* 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;
}

最佳答案

根据你原来的想法,我稍微修改了一下。这非常适合我的场景。

已编辑:来自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 值,只需输入目标数组,该函数将创建一个全新的数组,并使用它来创建所需的结构,而原始数组保持不变。

关于javascript - 解析嵌套数组对象,并创建一个新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com