【问题标题】:Traverse a json object recursively Results in "uncaught syntaxError: Illegal return statement"递归遍历 json 对象导致“未捕获的语法错误:非法返回语句”
【发布时间】:2015-10-07 06:59:38
【问题描述】:

我第一次尝试以递归方式浏览 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

【问题讨论】:

  • 在您的实际环境中,您是否将其视为承诺?我认为 async 可能会咬你的尾巴。
  • 在我的环境中,它不是一个promise,它只是作为一个方法写在我的集合(骨干)中。我一直被教导异步编码,我想我什至不知道如何同步编码哈哈。我应该使用 try/catch 或类似的东西吗? :)
  • 我把调试器放在你可以在控制台中调用“return obj”的地方,你会看到错误。即使使用“use strict”,它也会默默地失败。

标签: javascript json recursion traversal


【解决方案1】:

这里的问题是你实际上并没有返回任何东西,你必须从代码中的所有函数调用中返回一些东西。

最简单的解决方法是存储结果并在结果未定义时返回它。

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;
                }
            }
        }
    }
};

修改后的代码笔可以工作

请注意,我还对 findGroupId 进行了一些改进,删除了 checkForId 并将其放在“循环”之外,否则你会一遍又一遍地重新定义它。

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

【讨论】:

  • 非常感谢!这就是问题所在!我知道这与向每个函数调用添加返回有关!我不知道我需要将 checkForId 函数移到 findGroupId 之外。谢谢! :D
  • 其实不用移到外面,只是我做了一点优化。主要问题是你只是调用函数而不是在这里返回结果:for (var x = 0 ; x &lt;= obj[i].length ; x++) { findGroupId(obj[i], id);} 和这里if (typeof obj[i] === 'object') { findGroupId(obj[i], id);}
  • 再次感谢您!我学到了很多!我会留下这个密码笔,以便人们可以看到它。我创建了另一个显示此处找到的正确解决方案:codepen.io/eaglejs/pen/QbBKGK 我还编辑了原始帖子,以便人们可以更轻松地找到它。
猜你喜欢
  • 2020-02-09
  • 2016-05-16
  • 2013-11-26
  • 1970-01-01
  • 2011-12-08
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
相关资源
最近更新 更多