我有一个对象数组,可以包含相同对象类型的子对象,如下所示:
var exampleArray = [
{
alias: 'alias1',
children: [
{
alias: 'child1'
},
{
alias: 'child2',
children: [
{
alias: 'child4'
},
{
alias: 'child5'
}
]
},
{
alias: 'child3'
}
]
},
{
alias: 'alias2'
},
{
alias: 'alias3',
children: [
{
alias: 'child6'
},
{
alias: 'child7'
}
]
}
];
基础对象具有其他属性,但它们对手头的问题并不重要。现在,我们假设对象可以是:
{
alias: 'string',
children: []
}
孩子是可选的。
我正在寻找使用像这样的对象来管理某些事情的最佳方法/最快方法。我已经创建了一些递归方法来完成我想要的一些事情,但我想知道是否有更好的方法来执行以下任务:
目前,我以递归方式执行此操作,但鉴于此数组可以有限增长,递归方法最终会达到堆栈限制。
getParent(arr,alias) - 我需要能够获取包含具有给定别名的元素的父级。鉴于别名'对于整个数组来说是唯一的,永远不会有两个相同的别名。我现在再次递归地做这个,但我想找到更好的方法来做到这一点。
deleteObject(arr,alias) - 我不确定目前如何完成这个。我需要能够传递一个数组和一个别名,并从给定的数组中删除该对象(及其所有子对象)。我开始了这样做的递归方法,但停了下来,决定在这里发帖。
我正在使用Node.js并且可以使用lodash更快的方法。我还不是新手,所以我不确定是否有更好的方法来处理像这样的大规模数组。
答案 0 :(得分:1)
回到FORTRAN不支持递归的时代,通过更改数据集来模拟“递归”级别,实现了类似的效果。将此原则应用于示例对象结构,可以编写通过其“别名”(名称或id由另一个单词)查找对象的函数,而不会像这样递归:
function findAlias( parent, alias) // parent object, alias value string
{ function frame( parent)
{ return {parent: parent, children: parent.children,
index: 0, length: parent.children.length};
}
var stack, tos, child, children, i;
stack = [];
if( parent.children)
stack.push( frame( parent));
search:
while( stack.length)
{ tos = stack.pop(); // top of generation stack
children = tos.children;
for( i = tos.index; i < tos.length; ++i)
{ child = children[i];
if( child.alias == alias)
{ return { parent: tos.parent, child: child, childIndex: i}
}
if( child.children)
{ tos.index = i + 1;
stack.push(tos); // put it back
stack.push( frame(child));
continue search;
}
}
}
return null;
}
简而言之,最终会创建一堆小数据对象,这些对象在同一个函数中被推送和弹出,而不是进行递归调用。上面的示例返回带有parent
和child
对象值的对象。子值是具有提供的别名属性的值,父对象是具有子children
数组的父对象。
如果找不到别名,则返回null,因此可以用于hasAlias
功能。如果它不返回null,则执行getParent
功能。但是,您必须创建根节点:
// create a rootnode
var rootNode = { alias: "root", children: exampleArray};
var found = findAlias(rootNode, "alias3");
if( found)
{ console.log("%s is a child of %s, childIndex = %s",
found.child.alias, found.parent.alias, found.childIndex);
}
else
console.log("not found");
<小时/> [编辑:添加childIndex以搜索返回对象,更新测试示例代码,添加结论。]
<强>结论强>
当支持树行走应用程序时,使用递归函数调用在自记录代码和可维护性方面是有意义的。如果可以证明它在减小体积压力测试中减少服务器负载方面具有显着优势,但需要合理的文档,那么非递归变体可能会为它自己付出代价。
无论内部编码如何,返回具有父,子和子索引值详细信息的对象的树行走函数可以通过减少执行的树木总数来提高整体程序效率:
hasAlias
函数答案 1 :(得分:1)
保证最快的方式显然是
- an index for the aliases (thats actually a unique id)
- have a parent backlink on each child item (if it has a parent)
你查看id索引
var index = {}
(function build(parent) {
index[parent.alias] = parent;
(parent.children || []).forEach( item => {
item.parent = parent
build(item)
})
})(objectRoot)
function hasAlias(alias) { return alias in index }
function getAlias(alias) { return index[alias] }
function getParent(alias) { return index[alias] && index[alias].parent}
删除别名意味着从索引中删除它及其子项以及仍保留在索引中的父项
function deleteAlias(alias) {
function deleteFromIndex(item) {
delete index[item.alias]
(item.children || []).forEach(deleteFromIndex)
}
var item = index[alias]
item.parent.children.splice(item.parent.children.indexOf(item))
deleteFromIndex(item)
}
答案 2 :(得分:-1)
我可能会略微区别对待您的主阵列,并将其保持为引用其他项目的扁平数组,而不是完全合并它们。
var flat = [
{
alias : "str1",
children : [ flat[1], flat[2] ],
parent : null
},
{
alias : "str1",
children : [],
parent : flat[0]
},
{
alias : "str1",
children : [],
parent : flat[0]
}
]
这是一种“linked list”方法。链接列表有利有弊,但您可以快速迭代所有项目。