Vitalii Polishchuk 3 years ago
parent
commit
d365d9a702
2 changed files with 159 additions and 0 deletions
  1. 16 0
      js/05-js-objects-in-json/index.html
  2. 143 0
      js/05-js-objects-in-json/js/main.js

+ 16 - 0
js/05-js-objects-in-json/index.html

@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+
+<body>
+
+    <script src="js/main.js"></script>
+</body>
+
+</html>

+ 143 - 0
js/05-js-objects-in-json/js/main.js

@@ -0,0 +1,143 @@
+//3 persons
+let a = {
+    name: "Petro",
+    surname: "Petrov",
+}
+let b = {
+    name: "Ivan",
+    surname: "Ivanov",
+}
+let c = {
+    name: "Nikita",
+    surname: "Nikitin",
+}
+
+//different fields
+a.age = 21;
+b.sex = "male";
+c.fathername = "Ivanovi4"
+
+//fields check
+let optionalField = ["age", "sex", "fathername"];
+let userChoise = prompt("Выберите массив (a, b, c)");
+
+if (userChoise === "a") {
+    for (let i of optionalField) {
+        if (i in a) {
+            alert("Необязательное поле в массиве a: " + i);
+        }
+    }
+} else if (userChoise === "b") {
+    for (let i of optionalField) {
+        if (i in b) {
+            alert("Необязательное поле в массиве b: " + i);
+        }
+    }
+} else if (userChoise === "c") {
+    for (let i of optionalField) {
+        if (i in c) {
+            alert("Необязательное поле в массиве c: " + i);
+        }
+    }
+} else {
+    alert("Неправильный ввод")
+}
+
+//array of persons
+let persons = [a, b, c];
+
+let d = { name: "Katya", surname: "Popova" };
+
+persons[persons.length] = d;
+
+//loop of persons
+for (let i in persons) {
+    console.log(persons[i]);
+}
+
+//loop of name and surname !!!!!!!!!!!!!!!
+for (let i in persons) {
+    console.log(persons[i]["name"], persons[i]["surname"])
+}
+
+//loop of loop of values
+for (let i in persons) {
+    for (let j in persons[i]) {
+        console.log(persons[i][j]);
+    }
+}
+
+//fullName
+for (let i in persons) {
+    if (persons[i]["fathername"]) {
+        persons[i].fullName = persons[i]["name"] + persons[i]["fathername"] + persons[i]["surname"];
+    } else {
+        persons[i].fullName = persons[i]["name"] + persons[i]["surname"];
+    }
+}
+
+//serialize
+JSON.stringify(persons);
+
+//deserialize
+JSON.parse("[{\"name\":\"Petro\",\"surname\":\"Petrov\",\"age\":21,\"fullName\":\"PetroPetrov\"},{\"name\":\"Ivan\",\"surname\":\"Ivanov\",\"sex\":\"male\",\"fullName\":\"IvanIvanov\"},{\"name\":\"Nikita\",\"surname\":\"Nikitin\",\"fathername\":\"Ivanovi4\",\"fullName\":\"NikitaIvanovi4Nikitin\"},{\"name\":\"Katya\",\"surname\":\"Popova\",\"fullName\":\"KatyaPopova\"}]")
+
+//HTML
+
+let str = "<table border='1'>";
+str += `<tr><td>name</td><td>surname</td></tr>`;
+
+for (let i in persons) {
+    str += `<tr><td>${persons[i]["name"]}</td><td>${persons[i]["surname"]}</td></tr>`;
+}
+str += "</table>";
+
+console.log(str);
+document.write(str);
+
+//HTML optional fields
+let str2 = "<table border='1'>";
+str2 += `<tr><td>Name</td><td>Surname</td><td>Age</td><td>Sex</td><td>Fathername</td><td>FullName</td></tr>`;
+
+for (let i in persons) {
+    str2 += `<tr><td>${persons[i]["name"]}</td><td>${persons[i]["surname"]}</td><td>${persons[i]["age"] || ""}</td><td>${persons[i]["sex"] || ""}</td><td>${persons[i]["fathername"] || ""}</td><td>${persons[i]["fullName"]}</td></tr>`;
+}
+
+str2 += "</table>";
+
+console.log(str2);
+document.write(str2);
+
+
+//HTML tr color
+let str3 = "<table border='1'>";
+str3 += `<tr><td>Name</td><td>Surname</td><td>Age</td><td>Sex</td><td>Fathername</td><td>FullName</td></tr>`;
+
+for (let i in persons) {
+    if (i % 2 === 0) {
+        str3 += "<tr style='background: blue'>";
+    } else {
+        str3 += "<tr style='background: orange'>";
+    }
+    str3 += `<td>${persons[i]["name"]}</td><td>${persons[i]["surname"]}</td><td>${persons[i]["age"] || ""}</td><td>${persons[i]["sex"] || ""}</td><td>${persons[i]["fathername"] || ""}</td><td>${persons[i]["fullName"]}</td></tr>`;
+}
+
+str3 += "</table>";
+
+console.log(str3);
+document.write(str3);
+
+//HTML th optional
+
+
+//Задание на синий пояс
+
+//destruct array
+
+//destruct string
+
+//destruct 2
+
+//destruct 3
+
+//Задание на черный пояс