Javascript 用于访问嵌套数组中元素的递归函数和映射
形势Javascript 用于访问嵌套数组中元素的递归函数和映射,javascript,Javascript,形势 我有一个嵌套数组和一个要搜索的ID。像“A_02_02_01_03” 数组中的每个元素都有一个名为“children”的元素,即 排列 当我在第四层搜索时,我的方法变得相当长 示例数组 var tree= [ { "name": "i2", "children": [ { "name": "d1", "children": [], "id": "DW_02_01", "beschreibun
- 我有一个嵌套数组和一个要搜索的ID。像“A_02_02_01_03”
- 数组中的每个元素都有一个名为“children”的元素,即 排列
- 当我在第四层搜索时,我的方法变得相当长
var tree= [
{
"name": "i2",
"children": [
{
"name": "d1",
"children": [],
"id": "DW_02_01",
"beschreibung": "",
"table": []
},
{
"name": "d2",
"children": [
{
"name": "e1",
"children": [
{
"name": "a1",
"children": [],
"id": "A_02_02_01_01",
"beschreibung": "",
"table": []
},
{
"name": "a2",
"children": [],
"id": "A_02_02_01_02",
"beschreibung": "",
"table": []
},
{
"name": "a3",
"children": [],
"id": "A_02_02_01_03",
"beschreibung": "",
"table": []
}`enter code here`
],
"id": "E_02_02_01",
"beschreibung": "",
"table": []
},
{
"name": "e2",
"children": [],
"id": "E_02_02_02",
"beschreibung": "",
"table": []
}
],
"id": "DW_02_02",
"beschreibung": "",
"table": []
},
{
"name": "d3",
"children": [],
"id": "DW_02_03",
"beschreibung": "",
"table": []
}
],
"id": "IW_02",
"beschreibung": "",
"table": []
},
{
"name": "i3",
"children": [],
"id": "IW_03",
"beschreibung": "",
"table": []
}
]
构建ID
var daIW = "IW_02";
var daDW = "DW_02_02;
var daE = "E_02_02_01;
var daA = "A_02_02_01_03";
var iw_index = tree.findIndex(element => element.id == daIW);
var dw_index = tree[iw_index]["children"].findIndex(element => element.id == daDW);
var e_index = tree[iw_index]["children"][dw_index]["children"].findIndex(element => element.id == daE);
var a_index = tree[iw_index]["children"][dw_index]["children"][e_index]["children"].findIndex(element => element.id == daA);
获取我的所有索引
var daIW = "IW_02";
var daDW = "DW_02_02;
var daE = "E_02_02_01;
var daA = "A_02_02_01_03";
var iw_index = tree.findIndex(element => element.id == daIW);
var dw_index = tree[iw_index]["children"].findIndex(element => element.id == daDW);
var e_index = tree[iw_index]["children"][dw_index]["children"].findIndex(element => element.id == daE);
var a_index = tree[iw_index]["children"][dw_index]["children"][e_index]["children"].findIndex(element => element.id == daA);
访问我的元素
var elementName = tree[iw_index]["children"][dw_index]["children"][e_index]["children"][a_index].name;
问题
访问最深元素“a_02_02_01_03”然后搜索每个索引是否有较短的方法?您可能希望递归先搜索树深:
function search(array = [], id){
for(const node of array){
if(node.id === id) return node;
const sub = search(node.children, id);
if(sub) return sub;
}
}
因此,您可以:
const result = search(tree, "A_02_02_01_03");
const hash = createLookup(tree);
const result = hash.get("A_02_02_01_03");
如果要查找多个项目,最好建立一个存储所有id/节点对的哈希表,这样查找速度会非常快:
function createLookup(array, hash = new Map){
for(const node of array){
hash.set(node.id, node);
createLookup(node.children, hash);
}
return hash;
}
因此,您可以:
const result = search(tree, "A_02_02_01_03");
const hash = createLookup(tree);
const result = hash.get("A_02_02_01_03");
您可能希望递归先搜索树深:
function search(array = [], id){
for(const node of array){
if(node.id === id) return node;
const sub = search(node.children, id);
if(sub) return sub;
}
}
因此,您可以:
const result = search(tree, "A_02_02_01_03");
const hash = createLookup(tree);
const result = hash.get("A_02_02_01_03");
如果要查找多个项目,最好建立一个存储所有id/节点对的哈希表,这样查找速度会非常快:
function createLookup(array, hash = new Map){
for(const node of array){
hash.set(node.id, node);
createLookup(node.children, hash);
}
return hash;
}
因此,您可以:
const result = search(tree, "A_02_02_01_03");
const hash = createLookup(tree);
const result = hash.get("A_02_02_01_03");
我假设出于某种原因,您不能只搜索id为“A\u 02\u 02\u 01\u 03”
的条目。例如,由于某种原因,您需要其他ID
现在,您已经确认叶节点ID是唯一的,它只使用叶节点ID(例如,“A_02_02_01_03”
)将起作用。如果您有其他可用的ID,则可以通过避免访问不需要访问的节点来加快进程,但您必须有一个非常大的树才能实现这一点
如果有关系,这个答案仍然适用:
我可能会使用递归函数:
function find(node, ids, index = 0) {
const id = ids[index];
const entry = node.find(e => e.id == id);
if (!entry) {
return null;
}
++index;
return index < ids.length ? find(entry.children, ids, index) : entry;
}
这假定您希望该条目作为结果
实例:
var树=[
{
“名称”:“i2”,
“儿童”:[
{
“名称”:“d1”,
“儿童”:[…],
“id”:“DW_02_01”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“d2”,
“儿童”:[
{
“名称”:“e1”,
“儿童”:[
{
“名称”:“a1”,
“儿童”:[…],
“id”:“A_02_02_01_01”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“a2”,
“儿童”:[…],
“id”:“A_02_02_01_02”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“a3”,
“儿童”:[…],
“id”:“A_02_02_01_03”,
“beschreibung”:“,
“表格”:[]
}
],
“id”:“E_02_02_01”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“e2”,
“儿童”:[…],
“id”:“E_02_02_02”,
“beschreibung”:“,
“表格”:[]
}
],
“id”:“DW_02_02”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“d3”,
“儿童”:[…],
“id”:“DW_02_03”,
“beschreibung”:“,
“表格”:[]
}
],
“id”:“IW_02”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“i3”,
“儿童”:[…],
“id”:“IW_03”,
“beschreibung”:“,
“表格”:[]
}
];
var daIW=“IW_02”;
var daDW=“DW_02_02”;
var daE=“E_02_02_01”;
var daA=“A_02_02_01_03”;
函数查找(节点,ID,索引=0){
const id=ids[index];
const entry=node.find(e=>e.id==id);
如果(!输入){
返回null;
}
++指数;
返回索引我假设出于某种原因,您不能只搜索id为“A_02_02_01_03”
的条目。例如,由于某种原因,您需要其他ID
现在,您已经确认叶节点ID是唯一的,它只使用叶节点ID(例如,“A_02_02_01_03”
)将起作用。如果您有其他可用的ID,则可以通过避免访问不需要访问的节点来加快进程,但您必须有一个非常大的树才能实现这一点
如果有关系,这个答案仍然适用:
我可能会使用递归函数:
function find(node, ids, index = 0) {
const id = ids[index];
const entry = node.find(e => e.id == id);
if (!entry) {
return null;
}
++index;
return index < ids.length ? find(entry.children, ids, index) : entry;
}
这假定您希望该条目作为结果
实例:
var树=[
{
“名称”:“i2”,
“儿童”:[
{
“名称”:“d1”,
“儿童”:[…],
“id”:“DW_02_01”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“d2”,
“儿童”:[
{
“名称”:“e1”,
“儿童”:[
{
“名称”:“a1”,
“儿童”:[…],
“id”:“A_02_02_01_01”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“a2”,
“儿童”:[…],
“id”:“A_02_02_01_02”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“a3”,
“儿童”:[…],
“id”:“A_02_02_01_03”,
“beschreibung”:“,
“表格”:[]
}
],
“id”:“E_02_02_01”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“e2”,
“儿童”:[…],
“id”:“E_02_02_02”,
“beschreibung”:“,
“表格”:[]
}
],
“id”:“DW_02_02”,
“beschreibung”:“,
“表格”:[]
},
{
“名称”:“d3”,
“儿童”:[…],
“id”:“DW_02_03”,
“beschreibung”:“,
“表格”:[]
}
],
“id”:“IW_02”,
“beschreibung”:“,
“t