Drag and drop the json object recursively. Results in "uncaught syntaxError: Illegal return statement"
I am trying to traverse a JSON object for the first time recursively, and the code when run through the debugger seems to work until it tries to return the object when I find the group I am looking for. This is the error I am getting:
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)
Feel free to criticize any part of it as this is my first time trying to do this. :)
My example code is as follows:
'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);
Here is an example executable: http://codepen.io/eaglejs/pen/vOaZgd
Here is a fixed solution thanks to Pablo: http://codepen.io/eaglejs/pen/QbBKGK
source to share
The problem here is that you aren't actually returning anything, you need to return something from all the function calls in your code.
The simplest fix is to store the result and return it if it's not 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;
}
}
}
}
};
modified codepen that works
Note that I also improved findGroupId a bit by removing checkForId and placing it outside of the "loop", because otherwise you are overriding it over and over again.
source to share