最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to break out of a for loop inside a recursive function and return in Javascript? - Stack Overflow

programmeradmin4浏览0评论

I'm trying to pare keys of two objects, values of properties don't matter.

var obj1 = {
    foo: {
        abc: "foo.abc",
    },
    bar: {
        aaa: {
            bbb: "bar.aaa.bbb" // <-- difference
        }
    }
};

var obj2 = {
    foo: {
        abc: "foo.abc",
    },
    bar: {
        aaa: {
            ccc: "bar.aaac" // <-- difference
        }
    }
};
// function should return true if properties are identical, false otherwise
function pareObjProps(obj1, obj2) {
    for(var prop in obj1) {

        // when paring bar.aaa.bbb and bar.aaac
        // this does get logged, but the function doesn't return false
        if(!obj2.hasOwnProperty(prop)) {
            console.log("mismatch found");
            return false;
        }

        if(typeof(obj1[prop]) === "object") {
            pareObjProps(obj1[prop], obj2[prop]);
        }
    }

    // this always returns
    return true;
}

It seems that return false does not return from the top level function, but the recursive one.

So how can I return false when the whole matching function is done executing?

I'm trying to pare keys of two objects, values of properties don't matter.

var obj1 = {
    foo: {
        abc: "foo.abc",
    },
    bar: {
        aaa: {
            bbb: "bar.aaa.bbb" // <-- difference
        }
    }
};

var obj2 = {
    foo: {
        abc: "foo.abc",
    },
    bar: {
        aaa: {
            ccc: "bar.aaac" // <-- difference
        }
    }
};
// function should return true if properties are identical, false otherwise
function pareObjProps(obj1, obj2) {
    for(var prop in obj1) {

        // when paring bar.aaa.bbb and bar.aaac
        // this does get logged, but the function doesn't return false
        if(!obj2.hasOwnProperty(prop)) {
            console.log("mismatch found");
            return false;
        }

        if(typeof(obj1[prop]) === "object") {
            pareObjProps(obj1[prop], obj2[prop]);
        }
    }

    // this always returns
    return true;
}

It seems that return false does not return from the top level function, but the recursive one.

So how can I return false when the whole matching function is done executing?

Share Improve this question asked Feb 15, 2013 at 6:42 user1643156user1643156 4,54711 gold badges41 silver badges59 bronze badges 2
  • How do you know it diesn't return false? do you evalute the return value? – CloudyMarble Commented Feb 15, 2013 at 6:45
  • @MeNoMore var result = pareObjPros(obj1, obj2); console.log(result); always true... – user1643156 Commented Feb 15, 2013 at 6:50
Add a ment  | 

3 Answers 3

Reset to default 4

You're missing a return:

    if(typeof(obj1[prop]) === "object"
        && !pareObjProps(obj1[prop], obj2[prop]))
    {
       return false;
    }

Otherwise the result of recursive calls are going to be pletely ignored.

Try this:

function pareObjProps(obj1, obj2) {
    var result = true;
    for (var prop in obj1) {
        if (obj1.hasOwnProperty(prop) && !obj2.hasOwnProperty(prop)) {
            console.log("mismatch found");
            result = false;
        } else if (typeof(obj1[prop]) === "object") {
            result = pareObjProps(obj1[prop], obj2[prop]);
        }

        if (!result) {
            break;
        }
    }

    return result;
}

http://jsfiddle/gSYfy/4/

Yes, a function A called by function B cannot tell B to return. Some people have suggested returning the result of the recursive call, but that wouldn't give you the right result, as it would cause the program to return whether the first sub-object it found was identical, and not if all the objects are identical. I suggest you make this modification:

if(typeof(obj1[prop]) === "object") {
   if(!pareObjProps(obj1[prop], obj2[prop])){
      return false;
   }
}

This will make your program propagate a "false" as you wanted, but make it keep going if the result was "true".

发布评论

评论列表(0)

  1. 暂无评论