1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <header>
- string
- </header>
- <body>
- <script>
- function walker(parent) {
- let res = "";
- let isObject = parent != null && typeof parent === "object";
- let isArray = Array.isArray(parent);
- if (isArray) {
- res += "[";
- }
- else if (isObject) {
- res += "{";
- }
- else {
- res += `"${parent}"`;
- }
- if (isObject || isArray) {
- let childArr = [];
- for (let el in parent) {
- let childStr = `${walker(parent[el])}`; // разбор вложенного значения єлемента
- if (!isArray) {
- childStr = `"${el}":${childStr}`;
- }
- childArr.push(childStr);
- }
- res += childArr.join(",");
- }
- if (isArray) {
- res += "]";
- }
- else if (isObject) {
- res += "}";
- }
- return res;
- }
- const table = {
- tagName: 'table',
- attrs: {
- border: "1",
- },
- children: [
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["1x1"],
- },
- {
- tagName: "td",
- children: ["1x2"],
- },
- ]
- },
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["2x1"],
- },
- {
- tagName: "td",
- children: ["2x2"],
- },
- ]
- }
- ]
- }
- var objStr = walker(table);
- var obj = JSON.parse(objStr);
- document.write(objStr);
- var testObjStr = JSON.stringify(table);
- if (objStr != testObjStr)
- alert("Khren'");
- </script>
- </body>
|