Обьекты.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //Literals
  2. {
  3. var ford = {
  4. type: "hybrid",
  5. model: "fusion",
  6. power: 180,
  7. size: "4800/150/130",
  8. fuel: "petrol",
  9. };
  10. }
  11. //Literals expand
  12. var ford = {
  13. type: "hybrid",
  14. [prompt('Введите название ключа')]: [prompt('Введите параметр')].join(' '),
  15. power: 180,
  16. size: "4800/150/130",
  17. fuel: "petrol",
  18. };
  19. //Literals copy
  20. let property = prompt('Введите свойство')
  21. const obj = {...ford}
  22. obj.type = property
  23. console.log(obj)
  24. //Html tree
  25. let body = {
  26. tagName: 'body',
  27. children: [{
  28. tagName: 'div',
  29. children: [{
  30. tagName: 'span',
  31. children: ["Enter a data please:"]
  32. },
  33. {
  34. tagName: 'br',
  35. },
  36. {
  37. tagName: 'input',
  38. attrs: {
  39. children: {
  40. type:'text',
  41. id:'name',
  42. }
  43. },
  44. },
  45. {
  46. tagName: 'input',
  47. attrs: {
  48. children: {
  49. type:'text',
  50. id:'surname',
  51. }
  52. },
  53. }]
  54. },
  55. {
  56. tagName: 'div',
  57. children: [{
  58. tagName: 'button',
  59. attrs: {
  60. [attributes]: "ok",
  61. },
  62. children:['OK']
  63. },
  64. {
  65. tagName: 'button',
  66. attrs: {},
  67. }]
  68. }]
  69. }
  70. body.children[1].children[1].children=["Cancel"];
  71. body.children[1].children[1].attrs.id= "cancel";
  72. //Parent
  73. body.children[0].parent = body;
  74. body.children[1].parent = body;
  75. body.children[0].children[0].parent = body.children[0];
  76. body.children[0].children[1].parent = body.children[0];
  77. body.children[0].children[2].parent = body.children[0];
  78. body.children[0].children[3].parent = body.children[0];
  79. body.children[1].children[0].parent = body.children[1];
  80. body.children[1].children[1].parent = body.children[1];
  81. //destruct array
  82. {
  83. let arr = [1,2,3,4,5, "a", "b", "c"]
  84. let [odd1,even1,odd2,even2,odd3,...arr2]=arr
  85. console.log(odd1,even1,odd2,even2,odd3,arr2)//1 2 3 4 5 [ 'a', 'b', 'c' ]
  86. }
  87. //destruct string
  88. {
  89. let arr = [1, "abc"]
  90. let [number,arr2]=arr
  91. let [s1,s2,s3]=arr2
  92. console.log(number,s1,s2,s3)// 1 a b c
  93. }
  94. //destruct 2
  95. {
  96. let obj = {
  97. name: 'Ivan',
  98. surname: 'Petrov',
  99. children: [{name: 'Maria'}, {name: 'Nikolay'}]
  100. }
  101. let {children:[{name:name1},{name:name2}]}= obj
  102. console.log(name1,name2)//Maria Nikolay
  103. }
  104. //destruct 3
  105. {
  106. let arr = [1,2,3,4, 5,6,7,10]
  107. let {0:a,1:b,length}= arr
  108. console.log(a,b,length)//1 2 8
  109. }
  110. //Destructure и Change OK
  111. {
  112. let attributes = prompt('Введите название атрибута')// это к заданию Change OK
  113. let body = {
  114. tagName: 'body',
  115. children: [{
  116. tagName: 'div',
  117. children: [{
  118. tagName: 'span',
  119. children: ["Enter a data please:"]
  120. },
  121. {
  122. tagName: 'br',
  123. },
  124. {
  125. tagName: 'input',
  126. attrs: {
  127. children: {
  128. type:'text',
  129. id:'name',
  130. }
  131. },
  132. },
  133. {
  134. tagName: 'input',
  135. attrs: {
  136. type:'text',
  137. id:'surname',
  138. },
  139. }]
  140. },
  141. {
  142. tagName: 'div',
  143. children: [{
  144. tagName: 'button',
  145. attrs: {
  146. [attributes]: "ok",
  147. },
  148. children:['OK']
  149. },
  150. {
  151. tagName: 'button',
  152. children:["Cancel"],
  153. attrs: {
  154. id: "cancel"
  155. },
  156. }]
  157. }]
  158. }
  159. let {children:[{children:[{children:[textSpan]},{},{},{attrs:{id:textSurname}}]},{children:[{},{children:[textCancel]}]}]}=body//это к заданию Destructure
  160. console.log(textSpan,textCancel,textSurname)//Enter a data please: Cancel surname //это к заданию Destructure
  161. body.children[1].children[0].attrs.attributes= prompt('Введите значение атрибута');// это к заданию Change OK
  162. }
  163. //Copy delete
  164. {
  165. let ford = {
  166. type: "hybrid",
  167. model: "fusion",
  168. power: 180,
  169. size: "4800/150/130",
  170. fuel: "petrol",
  171. };
  172. const obj= {...ford}
  173. delete obj[prompt('Введите ключ для удаления')]
  174. }