Skip to content

Instantly share code, notes, and snippets.

@davestevens
Created January 22, 2012 22:07
Show Gist options
  • Save davestevens/1659076 to your computer and use it in GitHub Desktop.
Save davestevens/1659076 to your computer and use it in GitHub Desktop.
js isn't doing what i think it should be doing?
<html>
<head>
<script src="jquery.min.js"></script>
<script>
var rah;
var output = [];
/* i would assume that output would be the same as input
but the outcome is the a->a2->a3->a4 rather than a->a2, a->a3, a->a4
*/
function test() {
var input = '[{"name":"a","kind":"class","child":[{"name":"a2","kind":"class"},{"name":"a3","kind":"class"},{"name":"a4","kind":"class"}]}]';
rah = JSON.parse(input);
$(rah).each(function() {
switch($(this).attr('kind')) {
case 'class':
addClass($(this), output);
break;
default:
console.log('unknown: ' + $(this).kind);
break;
}
});
}
function addClass(c, p) {
var ac = new element($(c).attr('name'));
p.push(ac);
$($(c).attr('child')).each(function() {
switch($(this).attr('kind')) {
case 'class':
addClass($(this), ac['child']);
break;
default:
console.log('unknown: ' + $(c).kind);
break;
}
});
}
function element(name) {
this.name = name;
this.child = [];
}
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment