let someTree = { tagName: "table", //html tag children: [ //вложенные тэги { tagName: "tr", children: [ { tagName: "td", text: "some text 11", children: [ { tagName: "span", text: "some text 222", } ] }, { tagName: "td", text: "some text 33", } ] }, { tagName: "tr", children: [ { tagName: "td", text: "some text 44", children: [ { tagName: "span", text: "some text 555", } ] }, { tagName: "td", text: "some text 66", } ] } ], attrs: { border: 1, }, } let resultStr = ''; const objectWalker = (object) => { for (const attr in object.attrs) { resultStr += `<${object.tagName} ${attr}="${object.attrs[attr]}">`; } if(object.children) { for (let child of object.children) { resultStr += `<${child.tagName}>${child.text ? child.text : ''}`; objectWalker(child); } } return resultStr += `${object.tagName}>`; } console.log(objectWalker(someTree)); //
some text 11some text 222 | some text 33 |
some text 44some text 555 | some text 66 |