[英]javascript convert nested dictionary to tree like array structure
I have a sample input of strings in the form of - "abc", "ade", etc. 我有一个字符串输入示例,格式为-“ abc”,“ ade”等。
I want to convert the string parts separated by "." 我想转换以“。”分隔的字符串部分。 to be represented in a tree structure. 以树状结构表示。
So, I wrote the below function to create a nested dictionary object from such an array of input - 因此,我编写了以下函数,从这样的输入数组创建嵌套的字典对象-
function items_to_tree(items) {
var arr = {};
items.forEach(function(item){
var parts = item.split(".");
var last = parts.pop();
var cursor = arr;
parts.forEach(function(part){
if(!cursor[part]) cursor[part] = {};
cursor = cursor[part];
});
cursor[last] = {};
});
return arr;
}
So, for example If I give the below sample input to this function - 因此,例如,如果我将以下示例输入提供给此函数-
var items = ["a.b", "a.c", "b.c", "a.c", "a.c.d", "a.b.d"]
I get {"a":{"b":{"d":{}},"c":{"d":{}}},"b":{"c":{}}}
as expected. 我得到{"a":{"b":{"d":{}},"c":{"d":{}}},"b":{"c":{}}}
预期的{"a":{"b":{"d":{}},"c":{"d":{}}},"b":{"c":{}}}
。
But, I want the output to be in a format similar to this - 但是,我希望输出的格式与此类似-
[{name: "a", children: [{name: "b", children: [{name: "d", children: []}]}]}, {name: "c", children: [{name: "d", children: []}]}, {name: "b", children: [{name: "c", children: []}]}]
Is there any way the items_to_tree function can be modified to return such output, or can the intermediate output from the items_to_tree [the nested dictionary] be transformed into this tree like javascript object array. 是否可以通过任何方式修改items_to_tree函数以返回此类输出,或者可以将items_to_tree [嵌套字典]的中间输出转换为类似于javascript对象数组的树。
You could seach in the nested array for a given name and use that object or create a new one. 您可以在嵌套数组中搜索给定名称,然后使用该对象或创建一个新对象。
var items = ["ab", "ac", "bc", "ac", "acd", "abd"], result = []; items.forEach(function (path) { path.split('.').reduce(function (level, key) { var temp = level.find(({ name }) => key === name); if (!temp) { temp = { name: key, children: [] }; level.push(temp); } return temp.children; }, result); }); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I would write a second function for the final data conversion. 我将为最终数据转换编写第二个函数。 Your items_to_tree
is pretty generic and reusable. 您的items_to_tree
非常通用且可重用。 Converting this tree to the desired format can be done in a few lines: 只需几行即可将该树转换为所需格式:
function tree_to_format(tree) {
return Object
.keys(tree)
.map(k => ({ name: k, children: tree_to_format(tree[k]) }))
};
Now, you can compose the function you need by piping the result of items_to_tree
to tree_to_format
: 现在,您可以通过将tree_to_format
的结果items_to_tree
给tree_to_format
来组合所需的功能:
function items_to_tree(items) { var arr = {}; items.forEach(function(item){ var parts = item.split("."); var last = parts.pop(); var cursor = arr; parts.forEach(function(part){ if(!cursor[part]) cursor[part] = {}; cursor = cursor[part]; }); cursor[last] = {}; }); return arr; } function tree_to_format(tree) { return Object .keys(tree) .map(k => ({ name: k, children: tree_to_format(tree[k]) })) }; console.log( tree_to_format( items_to_tree(["ab", "ac", "bc", "ac", "acd", "abd"]) ) ); // or even: const compose = (f, g) => x => f(g(x)); const items_to_format = compose(tree_to_format, items_to_tree);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.