[英]how to find specific object form tree recursively
I want to find single json objec based on ID, from below tree. 我想从树下找到基于ID的单个json objec。 example - getObjeById(4)
, 示例getObjeById(4)
,
it should return obj from below tree. 它应该从树下面返回obj。 need help on this. 需要帮助。
data={
"mytree": {
"id": "dectree",
"dt": {
"choice": {
"id": 0,
"title": "Which color",
"description": "Choose color ?",
"choice": [
{
"id": 1,
"title": "Yellow",
"description": "Yellow ? ",
"choice": [
{
"id": 5,
"title": "Dark Yellow",
"description": "Dark Yellow ,
"choice": [
{
"id": 6,
"title": "id 6 yello",
"description": "<span> last leaf for yello </span>"
}]
},
{
"id": 4,
"title": "Light Yellow",
"description": "Light Yellow
}
]
},
{
"id": 2,
"title": "Red",
"description": "Red ?"
},
{
"id": 3,
"title": "Green",
"description": "Green
},
{
"id": 7,
"title": "white",
"description": "white color",
"choice": [
{
"id": 8,
"title": "id 8 white",
"description": "<span> last leaf for white </span>"
}]
}
]
}
}
}
}
Below is a snippet showcasing a recursive search function. 以下是展示递归搜索功能的代码段。
As warned, this function takes approximately 6 milliseconds to search this tree, about a third of a standard 60 fps frame. 请注意,此功能大约需要6毫秒来搜索该树,大约是标准60 fps帧的三分之一。
var data = { "mytree": { "id": "dectree", "dt": { "choice": { "id": 0, "title": "Which color", "description": "Choose color ?", "choice": [{ "id": 1, "title": "Yellow", "description": "Yellow ? ", "choice": [{ "id": 5, "title": "Dark Yellow", "description": "Dark Yellow", "choice": [{ "id": 6, "title": "id 6 yello", "description": "<span> last leaf for yello </span>" }] }, { "id": 4, "title": "Light Yellow", "description": "Light Yellow" }] }, { "id": 2, "title": "Red", "description": "Red ?" }, { "id": 3, "title": "Green", "description": "Green" }, { "id": 7, "title": "white", "description": "white color", "choice": [{ "id": 8, "title": "id 8 white", "description": "<span> last leaf for white </span>" }] }] } } } }; //Here comes the recursive function function searchTree(data, idLabel, idValue, results) { if (idLabel === void 0) { idLabel = "id"; } if (idValue === void 0) { idValue = "0"; } if (results === void 0) { results = []; } var keys = Object.keys(data); keys.forEach(function search(key) { if (typeof data[key] == "object") { results = searchTree(data[key], idLabel, idValue, results); } else { if (data[key] == idValue && key == idLabel) { results.push(data); } } }); return results; } console.log("Looking for 4:", searchTree(data, "id", "4")); console.log("Looking for 6:", searchTree(data, "id", "6"));
EDIT - flat structure 编辑-平面结构
An ideal structure would properly look more like this: 理想的结构应该看起来更像这样:
var data = [{ id: 1, title: "Yellow", description: "Yellow ? ", choices: [4, 5] }, { id: 2, title: "Red", description: "Red ?", choices: [] }, { id: 3, title: "Green", description: "Green", choices: [] }, { id: 4, title: "Light Yellow", description: "Light Yellow", choices: [] }, { id: 5, title: "Dark Yellow", description: "Dark Yellow", choices: [6] }, { id: 6, title: "id 6 yello", description: "<span> last leaf for yello </span>", choices: [] }, { id: 7, title: "white", description: "white color", choices: [8] }, { id: 8, title: "id 8 white", description: "<span> last leaf for white </span>", choices: [] }]; console.log("Get elements with id == 7", data.filter(function(i) { return i.id === 7 })[0]); console.log("Get elements with id == 2", data.filter(function(i) { return i.id === 1 })[0]); console.log("Get elements with id == 3 or id == 4", data.filter(function(i) { return i.id === 3 || i.id === 4 }));
With a structure like above, traversing the tree using filter becomes trivial. 使用上述结构,使用过滤器遍历树变得无关紧要。 Approximately 2 milliseconds calculation time on this structure and it should scale much better. 在此结构上大约需要2毫秒的计算时间,它的伸缩性应该更好。
From here, we could also easily sort our list or manipulate it in a bunch of ways using optimized, native functionality. 从这里开始,我们还可以使用优化的本机功能轻松地对列表进行排序或以多种方式对其进行操作。
is there any way to find immeida parent form node ? 有什么办法可以找到immeida父表单节点? I am geeting specific now example id : 5 and it maye be part of one parent whcih is id:3. 我现在正在查看特定的示例ID:5,它可能是ID:3的一个父级的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.