Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 递归一维嵌套数组以更新父节点_Javascript_Html_Vue.js_Recursion - Fatal编程技术网

Javascript 递归一维嵌套数组以更新父节点

Javascript 递归一维嵌套数组以更新父节点,javascript,html,vue.js,recursion,Javascript,Html,Vue.js,Recursion,我有一个一维嵌套数组: nestedObj: [ { id: 1, parentId: null, taskCode: '12', taskName: 'Parent', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []}, { id: 2, parentId: 1, taskCode: '12100', taskName: 'Child one', du

我有一个一维嵌套数组:

nestedObj: [
   { id: 1, parentId: null, taskCode: '12', taskName: 'Parent', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []},
   { id: 2, parentId: 1, taskCode: '12100', taskName: 'Child one', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []},
   { id: 3, parentId: 2, taskCode: '12200', taskName: 'SubChild one', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []},
   { id: 4, parentId: 1, taskCode: '12200', taskName: 'Child two', duration: 0, assignee: '', crewCount: 0, startDate: null, endDate: null, dependencies: []}
]
根据上述数据结构,下面说明了带有
taskName
的树状视图

-> Parent
        -> Child one
                   -> SubChild one
        -> Child two
这里是我的问题:当我更新子级的
startDate
时,它的直接父级的
startDate
应该更新为最小的
startDate
(它的所有子级),并且这个过程应该传播到根级。反之亦然,
endDate
(即所有子项的最大
startDate
)。如何使用递归实现这一点

注意:假设日期是时间戳


提前感谢

您需要的递归函数如下所示:

方法:{
调整父项(项目){
如果(!item.parentId)返回;//顶层,退出
const parent=this.nestedObj.find(o=>o.id==item.parentId);
const children=this.nestedObj.filter(o=>o.parentId===item.parentId);
parent.startDate=Math.min.apply(null,children.map(o=>o.startDate));
this.adjustParent(parent);//递归
}
}
您可以在
change
上调用它,例如:



问题有点宽泛。当你试图编写它的时候,你遇到了什么问题?它的结构是如此扁平,以至于我正在努力编写递归调用的函数。我不会编写整个功能,但是这个最小
startDate
递归的伪实现应该会给你你需要的东西。这个答案真的救了我一天。非常感谢你,丹