如何在JavaScript中获取关联数组中最深的对象



我想获得相关数组中最深的对象,如下所示。

interface Tree {
id: string
name: string
child?: Tree[]
}
const treeArray: Tree[] = [
{
id: "test", name: "test1", child: [
{ id: "Test#2", name: "@2ndtest" },
{
id: "test#2", name: "2ndtest", child: [
{ id: "Test#2#3", name: "@3rdtestTestTest" },
]
}
]
},
{
id: "testtest", name: "testtest2", child: [
{ id: "TestTest#2", name: "@2ndtesttest" }
]
},
{
id: "testtesttest", name: "testtesttest3"
}
]

在上面的";treeArray";,我想得到像{ id: "Test#2#3", name: "@3rdtestTestTest" }这样最深的物体。

所以,我尝试使用如下递归函数。

const deepestObject = (arr: Tree[]) => {
let arrayDeep = 0
let arrayNumber:any = []
arr.forEach((e,i) => {
if (e.child) {
arrayDeep += 1
deepestObject(e.child)
}
if(arr[i].id === e.id){
arrayNumber.push(arrayDeep)
}
});
return arrayNumber
}
console.log(deepestObject(treeArray))

我原以为这段代码输出的是[2,1,0],但输出的是[1,2,2]。若我在运行的代码中得到类似[2,1,0]的数组,我会选择数组中最大的数字,然后再次选择对象和递归函数。事实上,我真的很困惑,在我的情况下,我应该如何简单地获得最深的物体。有人建议我吗?

找到最深的路径,在那里获取对象:

您可以尝试先递归地找到对象中最深的路径,然后简单地在其末尾提取项。为此,内部helper()函数将遍历Tree对象及其子对象,并找到最深的嵌套项。结果是要从树结构中获取的索引数组,例如,[0, 1, 0]:第一个子(索引零(->第二个孩子(索引1(->第一个孩子(索引为零(。

TypeScript实现:

const deepestObject = (arr: Tree[]) => {
if (arr.length === 0)
return null;

function helper(el: Tree | Tree[], runningDepth: number[], deepest: number[]): number[] {
//if array - apply recursively on each element. return only the deepest of all
if (Array.isArray(el)) {
return el.reduce(
(lastDeepest, item, index) => helper(
item,                       //item
runningDepth.concat(index), //append to the running depth path
lastDeepest                 //pass the deepest we know of
), 
deepest
);
}
//terminal condition  - it's a Tree element and has no `.child` property
if (!el.child) {
//if the running total depth is greater, return that
if (runningDepth.length > deepest.length)
return runningDepth
else //or just return the last one
return deepest;
}
//recursively apply...
return helper(
el.child,     //...to all children...
runningDepth, //...pass the current path to append to...
deepest       //...and the current deepest path
);
}
const path = helper(arr, [], []);
const lastIndex = path.pop();
const parentArray = path.reduce((lastArr, nextIndex) => {
return lastArr[nextIndex].child!; //override the compiler check:
//we found this path, so it must exist
}, arr);
return parentArray[lastIndex!]; //override the compiler check:
//only case where lastIndex will be undefined is if
//the input is an empty array which is handled in the beginning
}

游乐场链接

JavaScript演示:

const deepestObject = (arr) => {
if (arr.length === 0)
return null;
function helper(el, runningDepth, deepest) {
if (Array.isArray(el)) {
return el.reduce((lastDeepest, item, index) =>
helper(item, runningDepth.concat(index), lastDeepest),
deepest
);
}
if (!el.child) {
if (runningDepth.length > deepest.length)
return runningDepth
else
return deepest;
}
return helper(el.child, runningDepth, deepest);
}
const path = helper(arr, [], []);
const lastIndex = path.pop();
const parentArray = path.reduce((lastArr, nextIndex) => lastArr[nextIndex].child, arr);
return parentArray[lastIndex];
}
const treeArray = [ { id: "test", name: "test1", child: [ { id: "Test#2", name: "@2ndtest" }, { id: "test#2", name: "2ndtest", child: [ { id: "Test#2#3", name: "@3rdtestTestTest" }, ] }, ] }, { id: "testtest", name: "testtest2", child: [ { id: "TestTest#2", name: "@2ndtesttest" }, ] }, { id: "testtesttest", name: "testtesttest3" } ];
console.log(deepestObject(treeArray));

直接返回最深的项目

与上面的想法类似。但是,helper并没有找到该项的路径,而是返回该项本身。它还需要跟踪它被发现的深度,因此它获取并返回一个带有深度计数器的对象和在该深度找到的对象。

TypeScript实现:

const deepestObject = (arr: Tree[]) => {
if (arr.length === 0)
return null;
function helper(el: Tree | Tree[], runningDepth: number, max: {depth: number, obj: Tree | null}): {depth: number, obj: Tree | null} {
//if array - apply recursively on each element. return only the deepest of all
if (Array.isArray(el)) {
return el.reduce(
({depth, obj}, item) => helper(
item,            //item
runningDepth +1, //increase the running depth
{depth, obj}     //pass the deepest we know of
), 
max
);
}
//terminal condition  - it's a Tree element and has no `.child` property
if (!el.child) {
//if the running total depth is greater, return new object
if (runningDepth > max.depth)
return {depth: runningDepth, obj: el};
else //or just return the last one
return max;
}
//recursively apply...
return helper(
el.child,       //...to all children...
runningDepth,   //...starting at the the current depth
max
);
}

const result = helper(arr, 0, {depth: 0, obj: null });
return result.obj;
}

游乐场链接

JavaScript演示:

const deepestObject = (arr) => {
if (arr.length === 0)
return null;
function helper(el, runningDepth, max) {
if (Array.isArray(el)) {
return el.reduce(
({depth, obj}, item) => helper(item, runningDepth + 1, {depth, obj}), 
max
);
}
if (!el.child) {
if (runningDepth > max.depth)
return {depth: runningDepth, obj: el};
else 
return max;
}
return helper(el.child, runningDepth, max);
}
const result = helper(arr, 0, {depth: 0, obj: null });
return result.obj;
}
const treeArray = [ { id: "test", name: "test1", child: [ { id: "Test#2", name: "@2ndtest" }, { id: "test#2", name: "2ndtest", child: [ { id: "Test#2#3", name: "@3rdtestTestTest" }, ] }, ] }, { id: "testtest", name: "testtest2", child: [ { id: "TestTest#2", name: "@2ndtesttest" }, ] }, { id: "testtesttest", name: "testtesttest3" } ];
console.log(deepestObject(treeArray))

最新更新