main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // 3 persons///////////////////////////////
  2. var a = {
  3. name: "Ivan",
  4. surname: "Ivanov",
  5. }
  6. var b = {
  7. name: "Petr",
  8. surname: "Petrov",
  9. }
  10. var c = {
  11. name: "Stepan",
  12. surname: "Stepanov",
  13. }
  14. // different fields////////////////////////
  15. var a = {
  16. name: "Ivan",
  17. surname: "Ivanov",
  18. sex: "male",
  19. }
  20. var b = {
  21. name: "Petr",
  22. surname: "Petrov",
  23. fathername: "Petrovich",
  24. }
  25. var c = {
  26. name: "Stepan",
  27. surname: "Stepanov",
  28. age: 33,
  29. }
  30. // fields check////////////////////////////
  31. var a = {
  32. name: "Ivan",
  33. surname: "Ivanov",
  34. sex: "male",
  35. }
  36. var b = {
  37. name: "Petr",
  38. surname: "Petrov",
  39. fathername: "Petrovich",
  40. }
  41. var c = {
  42. name: "Stepan",
  43. surname: "Stepanov",
  44. age: 33,
  45. }
  46. if ('sex' in a) {
  47. alert("found word in a!")
  48. }
  49. if ('fathername' in b) {
  50. alert("found word in b!")
  51. }
  52. if (typeof { age: 33 } === 'object') { alert("found in c!") }
  53. // array of persons////////////////////////
  54. var a = {
  55. name: "Ivan",
  56. surname: "Ivanov",
  57. sex: "male",
  58. }
  59. var b = {
  60. name: "Petr",
  61. surname: "Petrov",
  62. fathername: "Petrovich",
  63. }
  64. var c = {
  65. name: "Stepan",
  66. surname: "Stepanov",
  67. age: 33,
  68. }
  69. var persons = []
  70. persons.push(a, b, c, {
  71. name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
  72. })
  73. console.log(persons)
  74. // loop of persons//////////////////////////
  75. var a = {
  76. name: "Ivan",
  77. surname: "Ivanov",
  78. sex: "male",
  79. }
  80. var b = {
  81. name: "Petr",
  82. surname: "Petrov",
  83. fathername: "Petrovich",
  84. }
  85. var c = {
  86. name: "Stepan",
  87. surname: "Stepanov",
  88. age: 33,
  89. }
  90. var persons = []
  91. persons.push(a, b, c, {
  92. name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
  93. })
  94. for (let i = 0; i < persons.length; i++) {
  95. console.log(persons[i]);
  96. }
  97. // loop of name and surname//////////////////
  98. var a = {
  99. name: "Ivan",
  100. surname: "Ivanov",
  101. sex: "male",
  102. }
  103. var b = {
  104. name: "Petr",
  105. surname: "Petrov",
  106. fathername: "Petrovich",
  107. }
  108. var c = {
  109. name: "Stepan",
  110. surname: "Stepanov",
  111. age: 33,
  112. }
  113. var persons = []
  114. persons.push(a, b, c, {
  115. name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
  116. })
  117. for (let i = 0; i < persons.length; i++) {
  118. console.log(persons[i].name, persons[i].surname);
  119. }
  120. // loop of loop of values////////////////////
  121. var a = {
  122. name: "Ivan",
  123. surname: "Ivanov",
  124. sex: "male",
  125. }
  126. var b = {
  127. name: "Petr",
  128. surname: "Petrov",
  129. fathername: "Petrovich",
  130. }
  131. var c = {
  132. name: "Stepan",
  133. surname: "Stepanov",
  134. age: 33,
  135. }
  136. var persons = []
  137. persons.push(a, b, c, {
  138. name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
  139. })
  140. for (let i = 0; i < persons.length; i++) {
  141. for (let values in persons[i]) { console.log(persons[i][values]); }
  142. }
  143. // fullName///////////////////////////////////
  144. var a = {
  145. name: "Ivan",
  146. surname: "Ivanov",
  147. sex: "male",
  148. }
  149. var b = {
  150. name: "Petr",
  151. surname: "Petrov",
  152. fathername: "Petrovich",
  153. }
  154. var c = {
  155. name: "Stepan",
  156. surname: "Stepanov",
  157. age: 33,
  158. }
  159. var persons = []
  160. persons.push(a, b, c, {
  161. name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich"
  162. })
  163. for (let i = 0; i < persons.length; i++) {
  164. if (persons[i].name && persons[i].surname) {
  165. persons[i].fullName = persons[i].name + " " + persons[i].surname;
  166. }
  167. else if (persons[i].fathername) { persons[i].fullName = persons[i].name + " " + persons[i].fathername + " " + persons[i].surname; }
  168. console.log(persons[i].fullName)
  169. }
  170. // serialize//////////////////////////////////
  171. JSON.stringify(persons)
  172. // deserialize////////////////////////////////
  173. persons.push(JSON.parse('{"name":"Steve","surname":"Jobs","sex":"male"}'))
  174. console.log(persons)
  175. // HTML///////////////////////////////////////
  176. var str = "<table border='1'>\n"
  177. for (let i = 0; i < persons.length; i++) {
  178. console.log(persons[i]);
  179. str += `<tr><td>${persons[i].name}</td>\n
  180. <td>${persons[i].surname}</td></tr>\n`
  181. }
  182. str += "</table>"
  183. console.log(str)
  184. document.write(str)
  185. // HTML optional fields///////////////////////
  186. // var a = {
  187. // name: "Ivan",
  188. // surname: "Ivanov",
  189. // fathername: "Ivanovich",
  190. // sex: "male",
  191. // age: 33,
  192. // }
  193. // var b = {
  194. // name: "Petr",
  195. // surname: "Petrov",
  196. // fathername: "Petrovich",
  197. // sex: "male",
  198. // age: 33,
  199. // }
  200. // var c = {
  201. // name: "Stepan",
  202. // surname: "Stepanov",
  203. // fathername: "Stepanovich",
  204. // sex: "male",
  205. // age: 33,
  206. // }
  207. // var persons1 = []
  208. // persons1.push(a, b, c, {
  209. // name: "Vasya", surname: "Vasiliev", fathername: "Vasilievich", sex: "male", age: 33,
  210. // })
  211. // for (let i = 0; i < persons1.length; i++) {
  212. // console.log(persons1[i]);
  213. // }
  214. var str = "<table border='1'>"
  215. for (let i = 0; i < persons1.length; i++) {
  216. console.log(persons1[i]);
  217. str += `<tr><td>${persons1[i].name}</td><td>${persons1[i].surname}</td>
  218. <td>${persons1[i].fathername}</td><td>${persons1[i].age}</td><td>${persons1[i].sex}</td></tr>`
  219. }
  220. str += "</table>"
  221. console.log(str)
  222. document.write(str)
  223. // HTML tr color//////////////////////////////
  224. var str = "<table border='1'>"
  225. for (let i = 0; i < persons1.length; i++) {
  226. console.log(persons1[i]);
  227. if (i % 2 === 0) {
  228. str += `<tr style="background-color:maroon"><td>${persons1[i].name}</td><td>${persons1[i].surname}</td>
  229. <td>${persons1[i].fathername}</td><td>${persons1[i].age}</td><td>${persons1[i].sex}</td></tr>`
  230. }
  231. else {
  232. str += `<tr><td>${persons1[i].name}</td><td>${persons1[i].surname}</td>
  233. <td>${persons1[i].fathername}</td><td>${persons1[i].age}</td><td>${persons1[i].sex}</td></tr>`
  234. }
  235. }
  236. str += "</table>"
  237. console.log(str)
  238. document.write(str)
  239. // HTML th optional///////////////////////////