I'm having trouble with javascript recursion. Here is the code
_parseJson: function($object, $j_array) {
for (i = 0; i < $j_array.length; i++) {
var $el = document_designer.createElement($j_array[i]);
$object.append($el);
if ($j_array[i].elements.length > 0) {
this._parseJson($el, $j_array[i].elements);
}
}
}
Both functions (parseJson and createElement) are under the object called "document_designer". Here is the createElement
createElement: function(n) {
var $element = $('<' + n['tag'] + '/>');
document_designer.oi++;
if (n.id == null) {
$element.attr('id', '____element' + document_designer.oi);
}
document_designer.processElement($element, n); // you may ignore this line
return $element;
}
processElement function is not having problems. You may even ignore that line
document_designer._parseJson(document_designer.$preview, json_element)
// $preview is an created object in the `body`
and my parent JSON is : http://www.jsoneditoronline.org/?id=c99c77938e5edf91996e4bb267fd09b1
Root elements are creating and first main's sub childs but other root element's sub elements are not creating
-
1What's the problem you're having?Barmar– Barmar2016年10月13日 12:26:49 +00:00Commented Oct 13, 2016 at 12:26
-
Root elements are creating and first main's sub childs but other root element's sub elements are not creatingahmetertem– ahmetertem2016年10月13日 12:27:47 +00:00Commented Oct 13, 2016 at 12:27
-
@ahmetertem, please provide a sample of your json.autoboxer– autoboxer2016年10月13日 12:29:22 +00:00Commented Oct 13, 2016 at 12:29
-
@autoboxer jsoneditoronline.org/?id=c99c77938e5edf91996e4bb267fd09b1ahmetertem– ahmetertem2016年10月13日 12:29:42 +00:00Commented Oct 13, 2016 at 12:29
-
@autoboxer It's at the link he provided. It's really long.Barmar– Barmar2016年10月13日 12:29:43 +00:00Commented Oct 13, 2016 at 12:29
1 Answer 1
you forgot the var in
for (i = 0; i < $j_array.length; i++) {
So you are using the same global i for all calls
change to:
for (var i = 0; i < $j_array.length; i++) {
BTW , I like your code.
3 Comments
Explore related questions
See similar questions with these tags.