Vitalii Polishchuk 3 år sedan
förälder
incheckning
b0b10e5f3c
1 ändrade filer med 106 tillägg och 4 borttagningar
  1. 106 4
      js/05-js-objects-in-json/js/main.js

+ 106 - 4
js/05-js-objects-in-json/js/main.js

@@ -55,7 +55,7 @@ for (let i in persons) {
     console.log(persons[i]);
 }
 
-//loop of name and surname !!!!!!!!!!!!!!!
+//loop of name and surname
 for (let i in persons) {
     console.log(persons[i]["name"], persons[i]["surname"])
 }
@@ -128,9 +128,113 @@ console.log(str3);
 document.write(str3);
 
 //HTML th optional
+let str4 = "<table border='1'>";
 
+let arrayOfKeys = [];
+
+for (let i in persons) {                                            //запись ключей со всех персон
+    arrayOfKeys[i] = Object.keys(persons[i]);
+}
+
+arrayOfKeys = [...(new Set(arrayOfKeys.toString().split(",")))];    //делаем простой массив и удаляем повтор.ключи
+
+str4 += "<tr>"
+for (let key in arrayOfKeys) {
+    str4 += `<td>${arrayOfKeys[key]}</td>`
+}
+str4 += "</tr>"
+console.log(arrayOfKeys)
+
+for (let i in persons) {
+    str4 += "<tr>"
+    for (let key of arrayOfKeys) {
+        if (persons[i][key] === undefined) {
+            str4 += "<td> </td>"
+        } else {
+            str4 += `<td>${persons[i][key]}</td>`
+        }
+    }
+    str4 += "</tr>"
+}
+
+str4 += "</table>";
+
+console.log(str4);
+document.write(str4);
 
 //Задание на синий пояс
+var someTree = {
+    tagName: "table", //html tag
+    subTags: [ //вложенные тэги
+        {
+            tagName: "tr",
+            subTags: [
+                {
+                    tagName: "td",
+                    text: "some text",
+                },
+                {
+                    tagName: "td",
+                    text: "some text 2",
+                }
+            ],
+            attrs:
+            {
+                class: "b"
+            },
+        },
+        {
+            tagName: "tr",
+            subTags: [
+                {
+                    tagName: "td",
+                    text: "some text",
+                }
+            ],
+            attrs:
+            {
+                class: "a"
+            },
+        }
+    ],
+    attrs:
+    {
+        border: 1,
+        class: "bb"
+    },
+}
+
+
+let tagAttrs = "";
+let strSubTags = "";
+let subTagsAttrs = "";
+let subTagsAttrsInner = "";
+
+for (let key in someTree.attrs) {
+    tagAttrs += key + "=" + someTree.attrs[key] + " ";
+}
+
+for (let i of someTree.subTags) {
+
+    for (let j in i.attrs) {
+        subTagsAttrs = j + "=" + i.attrs[j] + " ";
+    }
+    strSubTags += `<${i.tagName + " " + subTagsAttrs}>`
+
+    if ("subTags" in i) {
+        for (let k of i.subTags) {
+            for (let l in k.attrs) {
+                subTagsAttrsInner = l + "=" + k.attrs[l] + " ";
+            }
+            strSubTags += `<${k.tagName + " " + subTagsAttrsInner}>${k.text}</${k.tagName}>`
+        }
+    }
+    strSubTags += `</${i.tagName}>`
+}
+
+let str = `<${someTree.tagName + " " + tagAttrs}>${strSubTags}</${someTree.tagName}>`;
+console.log(str)
+document.write(str);
 
 //destruct array
 let arr = [1, 2, 3, 4, 5, "a", "b", "c"];
@@ -154,6 +258,4 @@ let { children: [{ name: name1 }, { name: name2 }] } = obj;
 //destruct 3
 let arr3 = [1, 2, 3, 4, 5, 6, 7, 10];
 
-let [[a, b], length] = [arr3, arr3.length];
-
-//Задание на черный пояс
+let { length, 0: a, 1: b } = arr3;