Dz5js.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 3 persons
  12. let a = {
  13. name: "Andrey",
  14. surname: "Ivanov",
  15. }
  16. let b = {
  17. name: "Igor",
  18. surname: "Vasin",
  19. }
  20. let c = {
  21. name: "Oleg",
  22. surname: "Petrov",
  23. }
  24. //different fields
  25. let a = {
  26. name: "Andrey",
  27. surname: "Ivanov",
  28. fathername: "Andreevich",
  29. age: 21,
  30. sex: "male"
  31. }
  32. let b = {
  33. name: "Igor",
  34. surname: "Vasin",
  35. fathername: "Igorevich",
  36. age: 31
  37. }
  38. let c = {
  39. name: "Oleg",
  40. surname: "Petrov",
  41. fathername: "Olegovich"
  42. }
  43. //fields check
  44. let a = {
  45. name: "Andrey",
  46. surname: "Ivanov",
  47. fathername: "Andreevich",
  48. age: 21,
  49. sex: "male"
  50. }
  51. let b = {
  52. name: "Igor",
  53. surname: "Vasin",
  54. fathername: "Igorevich",
  55. age: 31
  56. }
  57. let c = {
  58. name: "Oleg",
  59. surname: "Petrov",
  60. fathername: "Olegovich"
  61. }
  62. if ("sex" in a) {
  63. alert(a.sex)
  64. } if ("age" in a) {
  65. alert(a.age)
  66. } if ("sex" in b) {
  67. alert(b.sex)
  68. } if ("age" in b) {
  69. alert(b.age)
  70. } if ("sex" in c) {
  71. alert(c.sex)
  72. } if ("age" in c) {
  73. alert(c.age)
  74. }
  75. //array of persons
  76. let arraypersons = [a, b, c, { name: 'Daniil', surname: 'Kovalev', fathername: "Alexandrovich", age: 30 }]
  77. //loop of persons
  78. let persons = [
  79. a = {
  80. name: "Andrey",
  81. surname: "Ivanov",
  82. fathername: "Andreevich",
  83. age: 21,
  84. sex: "male"
  85. },
  86. b = {
  87. name: "Igor",
  88. surname: "Vasin",
  89. fathername: "Igorevich",
  90. age: 31
  91. },
  92. c = {
  93. name: "Oleg",
  94. surname: "Petrov",
  95. fathername: "Olegovich"
  96. }
  97. ]
  98. for (i = 0; i < persons.length; i++) {
  99. console.log(persons[i].name + " " + persons[i].surname)
  100. }
  101. //loop of loop of values
  102. let persons = [
  103. a = {
  104. name: "Andrey",
  105. surname: "Ivanov",
  106. fathername: "Andreevich",
  107. age: 21,
  108. sex: "male"
  109. },
  110. b = {
  111. name: "Igor",
  112. surname: "Vasin",
  113. fathername: "Igorevich",
  114. age: 31
  115. },
  116. c = {
  117. name: "Oleg",
  118. surname: "Petrov",
  119. fathername: "Olegovich"
  120. }
  121. ]
  122. for (i = 0; i < persons.length; i++) {
  123. for (let key in persons[i]) {
  124. console.log(persons[i][key])
  125. }
  126. }
  127. //fullName
  128. let persons = [
  129. a = {
  130. name: "Andrey",
  131. surname: "Ivanov",
  132. fathername: "Andreevich",
  133. age: 21,
  134. sex: "male"
  135. },
  136. b = {
  137. name: "Igor",
  138. surname: "Vasin",
  139. fathername: "Igorevich",
  140. age: 31
  141. },
  142. c = {
  143. name: "Oleg",
  144. surname: "Petrov",
  145. fathername: "Olegovich"
  146. }
  147. ]
  148. for (let newkey of persons) {
  149. if (newkey.name && newkey.surname) {
  150. newkey.fullName = newkey.name + ' ' + newkey.surname;
  151. }
  152. } console.log(persons)
  153. //serialize
  154. JSON.stringify(persons)
  155. '[{"name":"Andrey","surname":"Ivanov","fathername":"Andreevich","age":21,"sex":"male"},{"name":"Igor","surname":"Vasin","fathername":"Igorevich","age":31},{"name":"Oleg","surname":"Petrov","fathername":"Olegovich"}]'
  156. //deserialize
  157. '{"name":"Anna","surname":"Petrova","fathername":"Igorevna","age":24}'
  158. let d = '{"name":"Anna","surname":"Petrova","fathername":"Igorevna","age":24}'
  159. d = JSON.parse('{"name":"Anna","surname":"Petrova","fathername":"Igorevna","age":24}')
  160. persons[3] = d;
  161. console.log(persons)
  162. // HTML
  163. let persons = [
  164. a = {
  165. name: "Andrey",
  166. surname: "Ivanov",
  167. fathername: "Andreevich",
  168. age: 21,
  169. sex: "male"
  170. },
  171. b = {
  172. name: "Igor",
  173. surname: "Vasin",
  174. fathername: "Igorevich",
  175. age: 31
  176. },
  177. c = {
  178. name: "Oleg",
  179. surname: "Petrov",
  180. fathername: "Olegovich"
  181. },
  182. d = {
  183. name: "Anna",
  184. surname: "Petrova",
  185. fathername: "Igorevna",
  186. age: 24
  187. }
  188. ]
  189. let str = "<table border='1' cols = 4>"
  190. for (let i = 0; i < persons.length; i++) {
  191. str += `<tr><td>${persons[i].name}</td><td>${persons[i].surname}</td></tr>`
  192. }
  193. str += "</table>"
  194. console.log(str)
  195. document.write(str)
  196. //HTML optional fields
  197. let persons = [
  198. a = {
  199. name: "Andrey",
  200. surname: "Ivanov",
  201. fathername: "Andreevich",
  202. age: 21,
  203. sex: "male"
  204. },
  205. b = {
  206. name: "Igor",
  207. surname: "Vasin",
  208. fathername: "Igorevich",
  209. age: 31
  210. },
  211. c = {
  212. name: "Oleg",
  213. surname: "Petrov",
  214. fathername: "Olegovich"
  215. },
  216. d = {
  217. name: "Anna",
  218. surname: "Petrova",
  219. fathername: "Igorevna",
  220. age: 24
  221. }
  222. ]
  223. let str = "<table border='1' cols = 4>"
  224. for (let i = 0; i < 4; i++) {
  225. for (let key in persons[i]) {
  226. str += `<tr><td>${key}</td><td>${persons[i][key]}</td></tr>`
  227. }
  228. }str += "</table>"
  229. console.log(str)
  230. document.write(str)
  231. //HTML tr color
  232. let persons = [
  233. a = {
  234. name: "Andrey",
  235. surname: "Ivanov",
  236. fathername: "Andreevich",
  237. age: 21,
  238. sex: "male"
  239. },
  240. b = {
  241. name: "Igor",
  242. surname: "Vasin",
  243. fathername: "Igorevich",
  244. age: 31
  245. },
  246. c = {
  247. name: "Oleg",
  248. surname: "Petrov",
  249. fathername: "Olegovich"
  250. },
  251. d = {
  252. name: "Anna",
  253. surname: "Petrova",
  254. fathername: "Igorevna",
  255. age: 24
  256. }
  257. ]
  258. let str = "<table border='1' cols = 4>"
  259. for (let i = 0; i < 4; i++) {
  260. for (let key in persons[i]) {
  261. str += `<tr style = "color: green; ><td>${key}</td><td>${persons[i][key]}</td></tr>`
  262. }
  263. }str += "</table>"
  264. console.log(str)
  265. document.write(str)
  266. //destruct array
  267. if (key in a, b, c)
  268. //destruct array
  269. let arr = [1, 2, 3, 4, 5, "a", "b", "c"]
  270. let [odd1, even1, odd2, even2, odd3, ...abc] = arr;
  271. //destruct string
  272. let arr = [1, "abc"]
  273. let [number, ...abc] = arr;
  274. let [a, b, c] = "abc";
  275. let [s1, s2, s3] = [a, b, c];
  276. //destruct 2
  277. let obj = {
  278. name: 'Ivan',
  279. surname: 'Petrov',
  280. children: [{ name: 'Maria' }, { name: 'Nikolay' }]
  281. }
  282. let {
  283. children:
  284. [{ name: name1 },
  285. { name: name2 }
  286. ] } = obj;
  287. //destruct 3
  288. let number = [1, 2, 3, 4, 5, 6, 7, 10];
  289. let [a, b, { length = number.length }] = number
  290. </script>
  291. </body>
  292. </html>