1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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)); // <table border="1"><tr><td>some text 11<span>some text 222</span></td><td>some text 33</td></tr><tr><td>some text 44<span>some text 555</span></td><td>some text 66</td></tr></table>
|