将json对象排序到嵌套树中
sort json objects into nested tree
我正在从web服务中提取两个相关的对象——文件夹和电子邮件。文件夹有一个ID和一个父文件夹。ID属性,指示文件夹嵌套在哪个父文件夹下。电子邮件有一个CategoryID,指示它是哪个文件夹的子文件夹。
我已经成功地创建了一个将电子邮件嵌套在平面文件夹结构中的功能:
{
"folders": [
{
"name": "my emails",
"type": "folder",
"additionalParameters": {
"id": "174661",
"type": "email",
"parentID": "0"
},
"children": [
{
"name": "Test1",
"type": "item",
"additionalParameters": {
"id": "27502",
"subject": "Test"
}
},
{
"name": "Hello",
"type": "item",
"additionalParameters": {
"id": "27917",
"subject": "Hi!"
}
}
]
},
{
"name": "Test",
"type": "folder",
"additionalParameters": {
"id": "175620",
"type": "email",
"parentID": "174661"
},
"children": [
{
"name": "Test2",
"type": "item",
"additionalParameters": {
"id": "27891",
"subject": "Test"
}
}
]
},
{
"name": "SubFolder1",
"type": "folder",
"additionalParameters": {
"id": "175621",
"type": "email",
"parentID": "175620"
},
"children": [
{
"name": "Test2",
"type": "item",
"additionalParameters": {
"id": "27892",
"subject": "Test"
}
},
{
"name": "Test3",
"type": "item",
"additionalParameters": {
"id": "27893",
"subject": "Test"
}
}
]
},
{
"name": "SubFolder2",
"type": "folder",
"additionalParameters": {
"id": "175622",
"type": "email",
"parentID": "175620"
},
"children": [
{
"name": "Test4",
"type": "item",
"additionalParameters": {
"id": "27894",
"subject": "Test"
}
}
]
}
]
}
现在,我需要使用递归循环遍历所有文件夹,并将它们推入其父文件夹的子文件夹数组中。本质上是将树诉诸于n个级别。我可以忽略任何type=items,因为它们已经被适当地嵌套。只需要对那些类型为文件夹的文件进行排序。
有人实现过JSON递归函数来重建带有嵌套的JSON对象吗?
谢谢你的帮助。
您可以在不递归的情况下完成此操作。我曾经回答过一个类似的问题。我相信你可以使用同样的方法(假设你没有前向参考):
var idToNodeMap = {}; //Keeps track of nodes using id as key, for fast lookup
var root = null; //Initially set our root to null
//loop over data
for(var i = 0; i < data.folders.length; i++) {
var folder = data.folders[i];
//each node will have children, so let's give it a "children" poperty
folder.children = [];
//add an entry for this node to the map so that any future children can
//lookup the parent
idToNodeMap[folder.additionalParameters.id] = folder;
//Does this node have a parent?
if(folder.additionalParamters.parentID === "0") {
//Doesn't look like it, so this node is the root of the tree
root = folder;
} else {
//This node has a parent, so let's look it up using the id
parentNode = idToNodeMap[folder.additionalParamters.parentID];
//Let's add the current node as a child of the parent node.
parentNode.children.push(folder);
}
}
感谢Vivin。通过他的回答,我找到了一个最终奏效的方法的链接。这是最后的代码:
var arr = $this.folderArray;
// Define root
console.log("arr");
console.log(arr);
// Define tree
var tree = {
root: root
};
console.log('tree');
console.log(tree);
// Get parent of node (recursive)
var getParent = function (rootNode, rootId) {
console.log('rootnode');
console.log(rootNode);
console.log('rootId');
console.log(rootId);
if (rootNode.additionalParameters.id === rootId)
return rootNode;
for (var i = 0; i < rootNode.children.length; i++) {
var child = rootNode.children[i];
if (child.additionalParameters.id === rootId) return child;
if(child.children){
if (child.children.length > 0){
var childResult = getParent(child, rootId);
if (childResult != null) return childResult;
}
}
}
return null;
};
// Traverse data and build the tree
var buildTree = function(tree) {
for (var i = 0; i < arr.length; i++) {
var elem = arr[i];
if (elem.additionalParameters.parentID === "0")
continue;
//elem["children"] = [];
var rootId = elem.additionalParameters.parentID;
var parent = getParent(tree.root, rootId);
console.log("parent");
console.log(parent);
parent.children.push(elem);
// Debug info
// console.log("Elem: " + elem.name + " with parent_id: " + elem.parentAreaRef.id);
//console.log("Got parent with name: " + parent._id);
}
};
buildTree(tree);
相关文章:
- 如何对两个嵌套对象进行排序
- MongoDB按嵌套对象值排序
- 当draggable使嵌套可排序时调用Drop
- 对嵌套的JSON数组进行排序
- 无法读取属性“”;匹配“;(嵌套排序)
- 使用ramda.js中的嵌套字段进行排序
- JavaScript - 按嵌套值对 JSON 对象进行排序
- 如何在javascript中对嵌套对象的数组进行排序
- Javascript 递归地排序对象和嵌套对象以及数组
- 使用 Javascript 和/或 Underscore.js 对嵌套对象进行排序
- 使用 AngularJS 对包含@attributes的嵌套 JSON 数据进行排序
- 多元素嵌套列表的自定义排序
- 在嵌套的 ng 重复的内部循环中排序
- 对嵌套列表进行排序
- 按嵌套的隐藏输入值对
- 元素进行排序
- 根据嵌套值对角度 ng 重复列表进行排序
- 嵌套可排序.js toarray 不起作用
- 禁止嵌套排序混用
- Dojo增强网格嵌套排序无法排序
- 我可以使用嵌套排序()对嵌套数组进行排序吗