递归函数输出双重结果?

时间:2016-06-08 16:55:38

标签: javascript arrays recursion

我是编写递归函数的新手,我有点卡在某事上。我无法弄清楚为什么这个功能输出到控制台两次......看看:

keyName = 'code'

// check, recursively, if an object contains an array
let concatObject = function(obj) {
    // get the keys of the parent object
    let keys = Object.keys(obj);
    // for each child object in the parent object
    for (let i = 0; i < keys.length; i++) {
        // get the current child
        let key = keys[i];
        // get the keys of the current child
        let intKeys = Object.keys(obj[key]);
        // for each child object in the child object
        for (let p = 0; p < intKeys.length; p++) {
            // get the nested child
            let intKey = intKeys[p];
            // if it's an array, repeat above steps by calling current function
            if (!Array.isArray(obj[i][intKey])) {
                // if it's not an array, get the value according to the passed keyName, checking for undefined
                if (typeof obj[i][keyName] !== 'undefined') {
                    console.log(obj[i][keyName]);
                }
            } else {
                console.log('FOUND ARRAY');
                concatObject(obj[i][intKey]);
            }
        }
    }
};

宣传传递的对象(obj):

{
    code: "10", name: "Games",
    subCategories: [
        {code: "10_34", name: "Action"},
        {code: "10_35", name: "Adventure"},
        {code: "10_36", name: "Arcade"},
        {code: "10_37", name: "Board"},
        {code: "10_38", name: "Card"},
        {code: "10_39", name: "Casino"},
        {code: "10_40", name: "Casual"},
        {code: "10_41", name: "Dice"},
        {code: "10_42", name: "Educational"},
        {code: "10_43", name: "Family"},
        {code: "10_44", name: "Kids"},
        {code: "10_45", name: "Music"},
        {code: "10_46", name: "Puzzle"},
        {code: "10_47", name: "Racing"},
        {code: "10_48", name: "Role Playing"},
        {code: "10_49", name: "Shooter"},
        {code: "10_50", name: "Simulation"},
        {code: "10_51", name: "Sports"},
        {code: "10_52", name: "Strategy"},
        {code: "10_53", name: "Trivia"},
        {code: "10_54", name: "Word"},
        {code: "10_55", name: "MMO"},
        {code: "10_57", name: "Gambling"},
        {code: "10_58", name: "Animals", animals: [
            {dog: "bark"},
            {cat: "meow"}
        ]}
    ]
},

输出,un(?)预期是:

10
10
FOUND ARRAY
10_34
10_34
10_35
10_35
10_36
10_36
10_37
10_37
10_38
10_38
10_39
10_39
10_40
10_40
10_41
10_41
10_42
10_42
10_43
10_43
10_44
10_44
10_45
10_45
10_46
10_46
10_47
10_47
10_48
10_48
10_49
10_49
10_50
10_50
10_51
10_51
10_52
10_52
10_53
10_53
10_54
10_54
10_55
10_55
10_57
10_57
10_58
10_58
FOUND ARRAY

过滤器正在运行(传递的keyName是'code',因此不应该显示双重嵌套的数组键名称)但是对于我当前的实现,我在这里使用的任何逻辑都会触发两次,我不希望这样!

帮助表示感谢,谢谢大家!

1 个答案:

答案 0 :(得分:0)

感谢@Cmaddux指出我逻辑中的缺陷。由于我迭代每个对象中存在的每个键,我需要确保该函数仅在满足正确条件时才起作用,即当传递的值keyName被执行时。

将我的IF条件改为此解决了它:

if ((typeof obj[i][keyName] !== 'undefined') && intKey === keyName)

经验教训:永远不要假设。我认为这是递归堆栈中的一些时髦的东西,当时,低,并且观察到人为错误。