Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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/1/angularjs/22.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 识别Json对象中的循环依赖项,并删除深度为2后的所有元素_Javascript_Angularjs_Json_Algorithm_Circular Dependency - Fatal编程技术网

Javascript 识别Json对象中的循环依赖项,并删除深度为2后的所有元素

Javascript 识别Json对象中的循环依赖项,并删除深度为2后的所有元素,javascript,angularjs,json,algorithm,circular-dependency,Javascript,Angularjs,Json,Algorithm,Circular Dependency,我有一个类似这样的json对象: var temp1 = { name: "AMC", children: [ { name: "cde", children: [ { name: "AMC", children: [ {

我有一个类似这样的json对象:

var temp1 = {
    name: "AMC",
    children: [
        {
            name: "cde",
            children: [
                {
                    name: "AMC",
                    children: [
                        {
                            name: "cde",
                            children: [
                                {
                                    name: "AMC",
                                    children: [
                                        //.............. continues as curcular depndency
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            name: "mnp",
            children: [
                {
                    name: "xyz",
                    children: []
                }
            ]
        }
    ]
}
由于这种循环依赖性,JSON.stringify失败了。 我已经做了足够多的谷歌搜索,以获得解决方案,但找不到太多的帮助

因此,这里基本上我想检测json对象中的循环依赖项,并向该对象添加一个新键,称为cricular:true并删除所有后续节点

下面是我正在查看的结果输出:

var temp1 = {
    name: "AMC",
    children: [
        {
            name: "cde",
            circular: true,
            children: [ // No children here as it is curcular dependency
            ]
        },
        {
            name: "mnp",
            children: [
                {
                    name: "xyz",
                    children: []
                }
            ]
        }
    ]
}
有一种方法,我认为可以解决这个问题,我可以循环遍历所有的子项,除非没有达到最大2级的子项,但是这样我会错过深度超过3级的有效子项


我希望我的问题是清楚的。如果没有,请告诉我,我将尝试进一步扩展此功能。

递归函数可解决此问题:

function check(stack,parent, obj){
    stack = stack || []; //stack contains a list of all previously occurred names
    var found = stack.find(function(parent){
        return (parent==obj.name && obj.children.length>0); //checks to see if the current object name matches any in the stack.
    });
    if(!found && obj.children.length>0){
        stack.push(obj.name); //adds the current object name to the list.
        obj.children.forEach(function(child){
            check(stack,obj, child);//recursively checks for all children.
        })
    }
    else if(found){
        parent.children=[];
        parent.circular=true;
        stack.pop(obj.name);
        return;
    }
    else{
        return;
    }
}
check([],temp1, temp1) 
这会导致传递的原始对象发生更改


希望这有帮助

我还没有证实这一点。。但从逻辑上看,这似乎是完美的。。所以接受这个。。我将再添加一条评论以确认这一点。谢谢,试用后请给我反馈!很乐意帮忙。:)I get error:RangeError:Maximum call stack size Excepended@ParamaOccurrence of Maximum call stack size Excepended意味着发生了太多函数调用,并且永远不会达到退出条件。您确定添加了返回语句吗?我正试图弄明白为什么会这样。是的。。我使用了与你完全相同的功能。。在我的输入中,json的根节点有20个子节点。。其中一个节点具有curcular依赖关系,它会在以防万一的情况下中断:有库在执行这种检测和实际解析时也会中断。