用于创建对象对象的递归逻辑

时间:2018-01-25 07:24:39

标签: javascript recursion logic

我已经给出了代码,输入和预期输出。我不知道如何用文字解释我的逻辑,所以我给出了最多的信息。在我的递归函数中需要帮助。 我不知道我的递归功能在哪里犯了错误。我有一个像:

的集合
this.fieldList = JSON.parse('{"State":{"type":"string","index":4,"members":{"Basuva":[3],"Tada":[2],"Miyar":[1],"Califo":[0]}},"Product":{"type":"string","index":3,"members":{"Van":[3,1],"Tempo":[2],"Bike":[0]}},"Date":{"type":"string","index":2,"members":{"FY 2005":[3,2,0],"FY 2006":[1]}},"Country":{"type":"string","index":1,"members":{"Canada":[3,2,1,0]}},"Amount":{"type":"number","index":0,"members":{"100":[2,0],"200":[3,1]}}}');

在那里,基于成员及其索引值,我需要与其嵌套成员相交。请在(JSFiddle:https://jsfiddle.net/pranath/0v4dntm4/6/

找到我的代码
intersectingMembersIndex = function ( keys, keyInd, position) {
            var rowMembers = {}, tempMembers = [];
           // if(!tempMembers.length)
               // tempMembers = [];
            this.fieldList = JSON.parse('{"State":{"type":"string","index":4,"members":{"Basuva":[3],"Tada":[2],"Miyar":[1],"Califo":[0]}},"Product":{"type":"string","index":3,"members":{"Van":[3,1],"Tempo":[2],"Bike":[0]}},"Date":{"type":"string","index":2,"members":{"FY 2005":[3,2,0],"FY 2006":[1]}},"Country":{"type":"string","index":1,"members":{"Canada":[3,2,1,0]}},"Amount":{"type":"number","index":0,"members":{"100":[2,0],"200":[3,1]}}}');
            var rlen = keys.length;
            var field = keys[keyInd].fieldName;
            var members = Object.keys(this.fieldList[field].members);
            var childrens = this.fieldList[field].members;
            for (var rw = 0, rln = members.length; rw < rln; rw++) {
                rowMembers.hasChild = keyInd < rlen;
                rowMembers.isDrilled = false;
                rowMembers.name = members[rw];
                rowMembers.type = rowMembers.hasChild ? 'All' : 'Single';
                let pindx = [];
                if(!(position && position.length)) {
                    pindx = childrens[members[rw]];
                } else {
                    pindx = position.filter(x => new Set(childrens[members[rw]]).has(x));
                }
                rowMembers.index = pindx;
                if (rlen-1 > keyInd) {
                    ++keyInd;
                    rowMembers.members = this.intersectingMembersIndex(keys, keyInd, rowMembers.index);
                    //keyInd--;
                }
                if(rlen-1 == keyInd) {
                    if (!rowMembers.members) {
                        rowMembers.members = [];
                    }
                    var tempCopy =  Object.assign({}, rowMembers);
                    if(pindx.length)
                        tempMembers.push(tempCopy);

                    //rowMembers.members = this.intersectingMembersIndex(keys, keyInd);
                } 
                if ( rw+1 >= rln) {
                    keyInd = 0;
                }
            }

            return tempMembers;
        };

        var keys = JSON.parse('[{"fieldName":"Product"},{"fieldName":"State"}]');
        var members = intersectingMembersIndex(keys, 0, []);

预期产出,

JSON.parse('[{"hasChild":true,"isDrilled":false,"name":"Van","type":"All","index":[3,1],"members":[{"hasChild":true,"isDrilled":false,"name":"Basuva","type":"All","index":[3],"members":[]},{"hasChild":true,"isDrilled":false,"name":"Miyar","type":"All","index":[1],"members":[]}]},{"hasChild":true,"isDrilled":false,"name":"Tempo","type":"All","index":[2],"members":[{"hasChild":true,"isDrilled":false,"name":"Tada","type":"All","index":[2],"members":[]}]},{"hasChild":true,"isDrilled":false,"name":"Bike","type":"All","index":[0],"members":[{"hasChild":true,"isDrilled":false,"name":"Califo","type":"All","index":[0],"members":[]}]}]')

实际输出,

JSON.parse(‘[{"hasChild":true,"isDrilled":false,"name":"Van","type":"All","index":[3,1],"members":[{"hasChild":true,"isDrilled":false,"name":"Basuva","type":"All","index":[3],"members":[]},{"hasChild":true,"isDrilled":false,"name":"Miyar","type":"All","index":[1],"members":[]}]},{"hasChild":true,"isDrilled":false,"name":"Tempo","type":"All","index":[2],"members":[{"hasChild":true,"isDrilled":false,"name":"Basuva","type":"All","index":[3],"members":[]},{"hasChild":true,"isDrilled":false,"name":"Miyar","type":"All","index":[1],"members":[]}]},{"hasChild":true,"isDrilled":false,"name":"Bike","type":"All","index":[0],"members":[{"hasChild":true,"isDrilled":false,"name":"Basuva","type":"All","index":[3],"members":[]},{"hasChild":true,"isDrilled":false,"name":"Miyar","type":"All","index":[1],"members":[]}]}]’)

当我取消注释“ - - keyInd”时它会产生正确的结果,但它不会作为集合返回。

1 个答案:

答案 0 :(得分:0)

  

取消注释“ - - keyInd”时会产生正确的结果,但不能   作为集合返回

因为你有一个导致空结果的If块。

删除此

if(rlen-1 == keyInd) //just the if not the code in between it

这是工作代码

intersectingMembersIndex = function ( keys, keyInd, position) {
            var rowMembers = {}, tempMembers = [];
           // if(!tempMembers.length)
               // tempMembers = [];
            this.fieldList = JSON.parse('{"State":{"type":"string","index":4,"members":{"Basuva":[3],"Tada":[2],"Miyar":[1],"Califo":[0]}},"Product":{"type":"string","index":3,"members":{"Van":[3,1],"Tempo":[2],"Bike":[0]}},"Date":{"type":"string","index":2,"members":{"FY 2005":[3,2,0],"FY 2006":[1]}},"Country":{"type":"string","index":1,"members":{"Canada":[3,2,1,0]}},"Amount":{"type":"number","index":0,"members":{"100":[2,0],"200":[3,1]}}}');
            var rlen = keys.length;
            var field = keys[keyInd].fieldName;
            var members = Object.keys(this.fieldList[field].members);
            var childrens = this.fieldList[field].members;
            for (var rw = 0, rln = members.length; rw < rln; rw++) {
                rowMembers.hasChild = keyInd < rlen;
                rowMembers.isDrilled = false;
                rowMembers.name = members[rw];
                rowMembers.type = rowMembers.hasChild ? 'All' : 'Single';
                let pindx = [];
                if(!(position && position.length)) {
                    pindx = childrens[members[rw]];
                } else {
                    pindx = position.filter(x => new Set(childrens[members[rw]]).has(x));
                }
                rowMembers.index = pindx;
                if (rlen-1 > keyInd) {
                    ++keyInd;
                    rowMembers.members = this.intersectingMembersIndex(keys, keyInd, rowMembers.index);
                    keyInd--;
                }
                    if (!rowMembers.members) {
                        rowMembers.members = [];
                    }
                    var tempCopy =  Object.assign({}, rowMembers);
                    if(pindx.length)
                        tempMembers.push(tempCopy);
                    //rowMembers.members = this.intersectingMembersIndex(keys, keyInd);
                if ( rw+1 >= rln) {
                    keyInd = 0;
                }
            }
            
            return tempMembers;
        };
        
        var keys = JSON.parse('[{"fieldName":"Product"},{"fieldName":"State"}]');
        var members = intersectingMembersIndex(keys, 0, []);
        debugger
        console.log(JSON.stringify(members));
        var expectedResult = '[{"hasChild":true,"isDrilled":false,"name":"Van","type":"All","index":[3,1],"members":[{"hasChild":true,"isDrilled":false,"name":"Basuva","type":"All","index":[3],"members":[]},{"hasChild":true,"isDrilled":false,"name":"Miyar","type":"All","index":[1],"members":[]}]},{"hasChild":true,"isDrilled":false,"name":"Tempo","type":"All","index":[2],"members":[{"hasChild":true,"isDrilled":false,"name":"Tada","type":"All","index":[2],"members":[]}]},{"hasChild":true,"isDrilled":false,"name":"Bike","type":"All","index":[0],"members":[{"hasChild":true,"isDrilled":false,"name":"Califo","type":"All","index":[0],"members":[]}]}]';
    
        console.log("IsEqual : ", JSON.stringify(members) === expectedResult);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>