递归遍历JSON对象导致“未捕获的语法错误:非法返回语句”。

3

我第一次尝试递归地遍历JSON对象,调试器运行代码时似乎能正常工作,直到在找到我要查找的groupId后尝试返回对象时出现了错误。以下是我收到的错误信息:

Uncaught SyntaxError: Illegal return statement
    at Object.InjectedScript._evaluateOn (<anonymous>:895:55)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluateOnCallFrame (<anonymous>:954:21)
    at findGroupId (http://s.codepen.io/boomerang/5978872e6c1baaa184d2e8ced60239201437139491273/index.html?editors=001:503:21)
    at findGroupId (http://s.codepen.io/boomerang/5978872e6c1baaa184d2e8ced60239201437139491273/index.html?editors=001:491:32)
    at findGroupId (http://s.codepen.io/boomerang/5978872e6c1baaa184d2e8ced60239201437139491273/index.html?editors=001:491:32)
    at findGroupId (http://s.codepen.io/boomerang/5978872e6c1baaa184d2e8ced60239201437139491273/index.html?editors=001:491:32)
    at findGroupId (http://s.codepen.io/boomerang/5978872e6c1baaa184d2e8ced60239201437139491273/index.html?editors=001:491:32)
    at findGroupId (http://s.codepen.io/boomerang/5978872e6c1baaa184d2e8ced60239201437139491273/index.html?editors=001:491:32)
    at findGroupId (http://s.codepen.io/boomerang/5978872e6c1baaa184d2e8ced60239201437139491273/index.html?editors=001:491:32)

这是我的第一次尝试,关于这个部分的任何批评都可以自由发表:)

以下是我的示例代码:

'use strict';

var findGroupId = function (obj, id) {
    var checkForId = function (key, obj) {
        if (key == id) {
            return true;
        } 
        return false;
    }; 
    if (typeof obj === 'object') {
        for (var i in obj) {
            if (typeof obj[i] === 'object') {
                findGroupId(obj[i], id);
            } else if (Array.isArray(obj[i])) {
                for (var x = 0 ; x <= obj[i].length ; x++) {
                    findGroupId(obj[i], id);
                }
            } else {
                var result = checkForId(obj[i], obj);
                if (result) {
                    debugger;
                    return obj;
                }
            }
        }
    }

};
var result = findGroupId(obj, "37078;1");
console.log(result);

这里有一个可执行的示例: http://codepen.io/eaglejs/pen/vOaZgd

感谢Pablo,这是修复后的解决方案: http://codepen.io/eaglejs/pen/QbBKGK


在你的实际环境中,你是否将其处理为一个 Promise?我认为异步可能会在这里给你带来麻烦。 - Alex
在我的环境中,它不是一个 Promise,它只是作为我的集合(backbone)中的一个方法编写。我一直被教导要异步编码,我甚至不知道如何同步编码哈哈。我应该使用 try/catch 或类似的东西吗? :) - eaglejs
我在代码中加入了一个调试器,你可以在控制台中调用“return obj”,然后就能看到错误信息。即使使用了“use strict”,它仍然会悄无声息地失败。 - eaglejs
1个回答

1
这里的问题是您实际上没有返回任何内容,您必须从代码中的所有函数调用中返回一些东西。
最简单的解决方法是存储结果并在其不为undefined时返回它。
function checkForId(key, obj, id) {
    if (key == id) {
        return true;
    }
    return false;
}
var findGroupId = function (obj, id) {
    if (typeof obj === 'object') {
        for (var i in obj) {
            if (typeof obj[i] === 'object') {
                var myresult = findGroupId(obj[i], id);
                if (myresult)
                    return myresult;
            } else if (Array.isArray(obj[i])) {
                for (var x = 0; x <= obj[i].length; x++) {
                    var myresult = findGroupId(obj[i], id);
                    if (myresult)
                        return myresult;
                }
            } else {
                var result = checkForId(obj[i], obj, id);
                if (result) {
                    return obj;
                }
            }
        }
    }
};

修改了可以工作的CodePen

请注意,我还略微改进了findGroupId函数,去掉了checkForId检查,并将其放在“循环”之外,否则您会一遍又一遍地重新定义它。

http://codepen.io/anon/pen/aOjwYW?editors=001


非常感谢!那就是问题所在!我知道它与在每个函数调用中添加返回有关!我不知道我需要将checkForId函数移动到findGroupId之外。谢谢!:D - eaglejs
实际上,您不必将其移动到外面,这只是我进行的一项小优化。主要问题在于您只是调用函数而不是返回结果,如下所示:for (var x = 0 ; x <= obj[i].length ; x++) { findGroupId(obj[i], id);}if (typeof obj[i] === 'object') { findGroupId(obj[i], id);} - leawp
再次非常感谢!我学到了很多!我会保留这个CodePen,让人们可以看到它。我创建了另一个展示正确解决方案的CodePen,链接在这里:http://codepen.io/eaglejs/pen/QbBKGK我还编辑了原始帖子,以便人们更容易找到它。 - eaglejs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接