var someTree = {
tagName: "table", //html tag
subTags: [ //вложенные тэги
{
tagName: "tr",
subTags: [
{
tagName: "td",
text: "some text",
},
{
tagName: "td",
text: "some text 2",
}
]
}
],
attrs:
{
border: 1,
},
}
Помогите дописать код, нужно с рекурсией, вот мой код:
var someTree = {
tagName: "table", //html tag
subTags: [ //вложенные тэги
{
tagName: "tr",
subTags: [
{
tagName: "td",
text: "some text",
},
{
tagName: "td",
text: "some text 2",
}
]
}
],
attrs:
{
border: 1,
},
}
function walker(node, nestedFieldName, deepness, callback){
callback(node, nestedFieldName, deepness);
if (nestedFieldName in node){
for (key in node[nestedFieldName]){
walker(node[nestedFieldName][key], nestedFieldName, deepness +1, callback)
}
}
}
var str = "";
walker(someTree, "subTags", 0, function(node, nestedFieldName, deepness){
str += " ".repeat(deepness) + node.tagName + "\n";
});
console.log(str);
Должно получиться:
<table border=1>
<tr>
<td>some text</td>
<td>some text 2</td>
</tr>
</table>