main.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //3 persons
  2. var a = {
  3. name: 'Vasya',
  4. surname: 'Pupkin'
  5. }
  6. var b = {
  7. name: 'Lesya',
  8. surname: 'Ivanova'
  9. }
  10. var c = {
  11. name: 'Petya',
  12. surname: 'Bobrov'
  13. }
  14. //different fields
  15. a["age"] = 10;
  16. b["age"] = 30;
  17. b["sex"] = 'female';
  18. c["fathername"] = 'Ivanovich';
  19. c["sex"] = 'male';
  20. //fields check
  21. let f = (field, myobject = {}) => {
  22. if (field in myobject)
  23. alert(`значение существующего поля в ассоц. массиве = ${myobject[field]}`)
  24. }
  25. // f("fathername", a)
  26. // f("sex", a)
  27. // f("age", a)
  28. // f("fathername", b)
  29. // f("sex", b)
  30. // f("age", b)
  31. // f("fathername", c)
  32. // f("sex", c)
  33. // f("age", c)
  34. //array of persons
  35. var persons = [a, b, c]
  36. //loop of persons
  37. for (let i = 0; i < persons.length; i++) {
  38. console.log(persons[i])
  39. }
  40. //loop of name and surname
  41. for (let i = 0; i < persons.length; i++) {
  42. console.log(persons[i].surname, persons[i].name)
  43. }
  44. //или
  45. for (let key in persons) {
  46. console.log(persons[key]["surname"], persons[key]["name"])
  47. }
  48. //loop of loop of values
  49. persons.forEach(function(element) {
  50. for (let key in element) {}
  51. console.log(Object.values(element));
  52. });
  53. //или
  54. for (let person in persons) {
  55. for (let key in persons[person]) {
  56. console.log(`${[key]} = ${persons[person][key]}`)
  57. }
  58. }
  59. //fullName
  60. let fullName = "";
  61. for (let person in persons) {
  62. if ("fathername" in persons[person]) {
  63. fullName = `${persons[person]["surname"]} ${persons[person]["name"]} ${persons[person]["fathername"]}`
  64. persons[person]["fullname"] = fullName;
  65. } else
  66. fullName = `${persons[person]["surname"]} ${persons[person]["name"]}`
  67. persons[person]["fullname"] = fullName;
  68. }
  69. console.log(persons)
  70. //serialize
  71. var jsonStringify = JSON.stringify(persons)
  72. console.log(jsonStringify);
  73. //deserialize
  74. var jsonParse = JSON.parse(JSON.stringify(persons[0]));
  75. persons.push(jsonParse);
  76. console.log(persons);
  77. //html
  78. var str = "<table border='1'>"
  79. for (let i = 0; i < persons.length; i++) {
  80. str += `<tr><td>${persons[i].name}</td>
  81. <td>${persons[i].surname}</td>
  82. </tr>`
  83. }
  84. str += "</table>"
  85. console.log(str)
  86. document.write(str)
  87. //html optional fields
  88. var str = "<table border='1'>"
  89. for (let person in persons) {
  90. str += `<tr>`
  91. for (let key in persons[person]) {
  92. str += `<td>${persons[person][key]} </td>`
  93. }
  94. }
  95. str += `</tr>`
  96. str += "</table>"
  97. console.log(str)
  98. document.write(str)
  99. //html tr color
  100. var str = "<table border='1'>"
  101. for (let i = 0; i < persons.length; i++) {
  102. (i % 2) ? str += `<tr>`: str += `<tr bgcolor="green">`
  103. for (let person in persons[i]) {
  104. str += `<td>${persons[i][person]} </td>`
  105. }
  106. str += `</tr>`
  107. }
  108. str += `</tr>`
  109. //html th optional
  110. var str = "<table border='1'>"
  111. for (let i in persons) {
  112. str += `<tr>`
  113. str += ` <td> person № ${i}`
  114. str += ` <td>`
  115. str += `<tr>`
  116. let keyLength = Object.keys(persons[i]).length;
  117. let keyObj = Object.keys(persons[i]);
  118. let valueObj = Object.values(persons[i])
  119. for (keyLength in valueObj) {
  120. str += `<th>${(keyObj[keyLength])} `
  121. str += `<td>${(valueObj[keyLength])} `
  122. str += `<tr>`
  123. str += `</th>`
  124. str += `</td>`
  125. }
  126. str += ` </td>`
  127. str += `</tr>`
  128. str += `</tr>`
  129. str += `</tr>`
  130. }
  131. str += "</table>"
  132. document.write(str)
  133. //html constructor Задание на синий пояс
  134. var body = {
  135. tagName: 'body',
  136. subTags: [{
  137. tagName: 'div',
  138. subTags: [{
  139. tagName: 'span',
  140. text: "Enter a data please:",
  141. },
  142. {
  143. tagName: 'br',
  144. },
  145. {
  146. tagName: 'input',
  147. attrs: {
  148. type: 'text',
  149. id: 'name'
  150. }
  151. },
  152. {
  153. tagName: 'input',
  154. attrs: {
  155. type: 'text',
  156. id: 'surname'
  157. }
  158. }
  159. ],
  160. },
  161. {
  162. tagName: 'div',
  163. subTags: [{
  164. tagName: 'button',
  165. text: "OK",
  166. attrs: {
  167. id: 'ok'
  168. }
  169. },
  170. {
  171. tagName: 'button',
  172. text: "Cancel",
  173. attrs: {
  174. id: 'cancel'
  175. }
  176. },
  177. ],
  178. },
  179. ],
  180. }
  181. var str = '';
  182. str += `<${body.tagName}>`
  183. for (let i of body.subTags) {
  184. str += `<${i.tagName}>`
  185. for (let ichild of i.subTags) {
  186. str += `<${ichild.tagName}>`
  187. for (let keys in ichild.subTags) {
  188. str += `${ichild.subTags[keys]} `
  189. }
  190. for (let keys in ichild.text) {
  191. str += `${ichild.text[keys]}`
  192. }
  193. for (let keys in ichild.attrs) {
  194. str += `<${ichild.attrs[keys]}>`
  195. }
  196. str += `</${ichild.subTags}>`
  197. str += `</${ichild.tagName}>`
  198. }
  199. }
  200. str += `</${body.tagName}>`
  201. document.write(str);
  202. //destruct array
  203. let arr = [1, 2, 3, 4, 5, "a", "b", "c"]
  204. let [odd1, even1, odd2, even2, odd3, ...array] = arr
  205. console.log('odd1=', odd1, 'even1=', even1, 'odd2=', odd2, 'even2=', even2, 'odd3=', odd3, 'array=', ...array)
  206. //destruct string
  207. let arr2 = [1, "abc"]
  208. let [number] = arr2
  209. console.log('number =', number)
  210. let [s1, s2, s3] = arr2[1];
  211. console.log('s1 =', s1, 's2 = ', s2, 's3=', s3)
  212. //destruct 2
  213. let obj = {
  214. name: 'Ivan',
  215. surname: 'Petrov',
  216. children: [{ name: 'Maria' }, { name: 'Nikolay' }]
  217. }
  218. let { children: [{ name: name1 }, { name: name2 }] } = obj
  219. console.log('name1=', name1, 'name2=', name2)
  220. //destruct 3
  221. let arr3 = [1, 2, 3, 4, 5, 6, 7, 10]
  222. let { a = arr3[0], b = arr3[1] } = arr3
  223. let { length = arr3.length } = arr3
  224. console.log(a, b, length);
  225. //Задание на черный пояс
  226. let predictArray = {
  227. 1: -1,
  228. 2: -1,
  229. 3: -1,
  230. 4: -1,
  231. }
  232. for (let keys in Object.keys(predictArray)) {
  233. console.log('predictValue', predictValue)
  234. var history = { 1: 1, 2: 2, 3: 1, 4: 1 }
  235. var predictValue = predictArray[history] // в predictValue то, что ввел последний раз пользователь после нажатий 1212
  236. var newValue = prompt("введите 1 или 2", "");
  237. predictArray[history] = newValue
  238. console.log(predictValue)
  239. }
  240. //сохраняем новый ввод
  241. //сдвигаем историю