DZ_Objects.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. Literals
  2. const cat =
  3. {
  4. "head":
  5. {
  6. "name": "cat's head",
  7. "ears": [{"name":"ear", "attrs":{"position":"left"}}, {"name":"ear", "attrs":{"position":"right"}}],
  8. "eyes": [{"name":"eye", "attrs":{"position":"left"}}, {"name":"eye", "attrs":{"position":"right"}}],
  9. "mouth": "cat's"
  10. },
  11. "body":
  12. {
  13. "name": "body",
  14. "paws":
  15. [
  16. {"name":"paw", "attrs":{"position":"left", "placement": "front"}},
  17. {"name":"paw", "attrs":{"position":"right", "placement": "front"}},
  18. {"name":"paw", "attrs":{"position":"left", "placement": "rear"}},
  19. {"name":"paw", "attrs":{"position":"right", "placement": "rear"}},
  20. ],
  21. "tail": "cat's tail"
  22. }
  23. }
  24. Literals expand
  25. cat[prompt("Enter property name")]=prompt("Enter property value");
  26. cat[prompt("Enter property name")]=prompt("Enter property value");
  27. Literals copy
  28. не понял задания
  29. html =
  30. {
  31. "tagName": "body",
  32. "children": [
  33. {
  34. "tagname": "div",
  35. "children": [
  36. {
  37. "tagName": "span",
  38. "children": [
  39. "Enter a data please:"
  40. ],
  41. },
  42. {
  43. "tagName": "br"
  44. },
  45. {
  46. "tagName": "input",
  47. "attrs": {
  48. "type": "text",
  49. "id": "name"
  50. }
  51. },
  52. {
  53. "tagName": "input",
  54. "attrs": {
  55. "type": "text",
  56. "id": "surname"
  57. }
  58. }
  59. ]
  60. },
  61. {
  62. "tagName": "div",
  63. "children": [
  64. {
  65. "tagName": "button",
  66. "attrs": {
  67. "id": "ok"
  68. },
  69. "children": [
  70. "OK"
  71. ]
  72. },
  73. {
  74. "tagName": "button",
  75. "attrs": {
  76. "id": "cancel"
  77. },
  78. "children": [
  79. "Cancel"
  80. ]
  81. }
  82. ]
  83. }
  84. ]
  85. }
  86. Html tree
  87. html.children[1].children[1].children[0]
  88. 'Cancel'
  89. html.children[0].children[3].attrs.id
  90. 'surname'
  91. Parent
  92. html.children[0].parent=html
  93. html.children[1].parent=html
  94. html.children[0].children[0].parent=html.children[0]
  95. html.children[0].children[1].parent=html.children[0]
  96. html.children[0].children[2].parent=html.children[0]
  97. html.children[0].children[3].parent=html.children[0]
  98. html.children[0].children[2].attrs.parent=html.children[0].children[2]
  99. html.children[0].children[3].attrs.parent=html.children[0].children[3]
  100. html.children[1].children[0].parent=html.children[1]
  101. html.children[1].children[1].parent=html.children[1]
  102. html.children[1].children[0].attrs.parent=html.children[1].children[0]
  103. html.children[1].children[1].attrs.parent=html.children[1].children[1]
  104. destruct array
  105. [odd1, even1, odd2, even2, odd3, ...letters] = arr
  106. destruct string
  107. [number, [s1, s2, s3]] = arr
  108. destruct 2
  109. let {children: [{name: name1}, {name: name2}]} = obj
  110. destruct 3
  111. let {0: a, 1: b, length}=arr
  112. Change OK
  113. html.children[1].children[0].attrs.test = prompt("enter attribute value")
  114. html.children[1].children[0].attrs[prompt("enter attribute name")] = prompt("enter attribute value")
  115. Destructure
  116. const {children: [{children: [{children}]}]}=html
  117. Destruct array
  118. let {0: odd1, 2: odd2, 4: odd3, 1: even1, 3: even2, ...letters} = arr
  119. Destruct string
  120. let {0: number, 1: {0: s1, 1: s2, 2: s3}} = arr
  121. destruct 2
  122. let {children: [{name: name1}, {name: name2}]} = obj //не вижу разницы в определении по сравнению с аналогичным выше
  123. Destruct 3
  124. let {0: a, 1: b, length}=arr //не вижу разницы в определении по сравнению с аналогичным выше
  125. Form
  126. obj = {
  127. "Name":"chevrolet chevelle malibu",
  128. "Cylinders":8,
  129. "Displacement":307,
  130. "Horsepower":130,
  131. "Weight_in_lbs":3504,
  132. "Origin":"USA",
  133. "in_production": false
  134. }
  135. const typesMap = {'boolean':'checkbox', 'number':'number', 'string':'text'};
  136. let str="<form>\n"
  137. for (var name in obj){
  138. value = obj[name];
  139. type = typeof value;
  140. htmlType = typesMap[type];
  141. if(htmlType=='text' && !isNaN(+value))
  142. htmlType = typesMap['number']
  143. str += ` <label>${name}: <input type="${htmlType}" value="${obj[name]}" /></label>\n`;
  144. }
  145. str+="<form>"
  146. document.write(str)
  147. alert(str)
  148. Table //Эту уже не успеваю писать for-ы - решил в 4 строчки. попробуй посмотреть, завтра объясню
  149. var arr = [
  150. {
  151. "Name":"chevrolet chevelle malibu",
  152. "Cylinders":8,
  153. "Displacement":307,
  154. "Horsepower":130,
  155. "Weight_in_lbs":3504,
  156. "Origin":"USA"
  157. },
  158. {
  159. "Name":"buick skylark 320",
  160. "Miles_per_Gallon":15,
  161. "Cylinders":8,
  162. "Displacement":350,
  163. "Horsepower":165,
  164. "Weight_in_lbs":3693,
  165. "Acceleration":11.5,
  166. "Year":"1970-01-01",
  167. },
  168. {
  169. "Miles_per_Gallon":18,
  170. "Cylinders":8,
  171. "Displacement":318,
  172. "Horsepower":150,
  173. "Weight_in_lbs":3436,
  174. "Year":"1970-01-01",
  175. "Origin":"USA"
  176. },
  177. {
  178. "Name":"amc rebel sst",
  179. "Miles_per_Gallon":16,
  180. "Cylinders":8,
  181. "Displacement":304,
  182. "Horsepower":150,
  183. "Year":"1970-01-01",
  184. "Origin":"USA"
  185. },
  186. ]
  187. names = Object.entries(arr.reduce((prev, next) => {return {...prev, ...next};} )).map(entry => entry[0]);
  188. str = "<tr>"+names.map(name => `<td>${name}<td>`).join('')+"</tr>";
  189. str += `<table>${arr.map(obj => `<tr>${names.map(name => `<td>${obj[name]||"-"}</td>`).join('')}</tr>`).join('')}</table>`
  190. document.write(str)