Обьекты.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. let key = prompt('Введите ключ для удаления')
  173. const {[key]:x, ...obj} = ford
  174. console.log(obj)
  175. }
  176. //Currency real rate
  177. {
  178. fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  179. .then(data => {
  180. let currency = prompt('Введите исходную валюту').toUpperCase()
  181. let currency2 = prompt('валюту, в которую происходит конвертация').toUpperCase()
  182. let sum = +prompt('Введите сумму в '+currency2)
  183. let rates1 = data.rates[currency]
  184. let rates2 = data.rates[currency2]
  185. data = (rates1/rates2)*sum
  186. if (currency && currency2 && sum && data){
  187. alert('Результат '+data+' '+currency)}
  188. else{alert('Ошибка')}
  189. })
  190. }
  191. //Currency drop down
  192. {
  193. fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  194. .then(data => {
  195. let key = data.rates
  196. key = Object.keys(key)
  197. key = key.map(x=> "<option>"+x+"</option>")
  198. key = '<select>'+key+'</select>'
  199. console.log(document.write(key))
  200. })
  201. }
  202. //Currency table
  203. {
  204. fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  205. .then(data => {
  206. let dataObj = data.rates
  207. let arrValues = Object.values(dataObj)
  208. let arrKeys = Object.keys(dataObj)
  209. let arrData = []
  210. let i = 0
  211. for(let valuesEl of arrValues){
  212. arrData.push(arrValues.map(x=>(valuesEl/x).toFixed(3)))
  213. arrData[i].unshift(arrKeys[i])
  214. i++
  215. }
  216. arrKeys.unshift('')
  217. arrData.unshift(arrKeys)
  218. let currencyTable = "<table border='1'>"
  219. for (let arrEl of arrData){
  220. currencyTable += '<tr>'
  221. for (let cell of arrEl){
  222. currencyTable += '<td>'+cell+'</td>'
  223. }
  224. currencyTable+= '</tr>'
  225. }
  226. currencyTable+= "</table>"
  227. document.write(currencyTable)
  228. })
  229. }
  230. //Form
  231. {
  232. const car = {
  233. "Name":"chevrolet chevelle malibu",
  234. "Cylinders":8,
  235. "Displacement":307,
  236. "Horsepower":130,
  237. "Weight_in_lbs":3504,
  238. "Origin":"USA",
  239. "in_production": false
  240. }
  241. let keysArrCar = Object.keys(car)
  242. let valuesArrCar = Object.values(car)
  243. let i = 0
  244. let strType
  245. let formObjCar = '<form>\n'
  246. for (let a of keysArrCar){
  247. if (typeof valuesArrCar[i]=='string'){
  248. strType = 'text'
  249. }
  250. else if(typeof valuesArrCar[i]=='boolean'){
  251. strType = 'checkbox'
  252. }
  253. else{strType=typeof valuesArrCar[i]}
  254. formObjCar += ' <label>'+a+': <input type="'+strType+'" value="'+valuesArrCar[i]+'"/></label>\n'
  255. i++
  256. }
  257. formObjCar += '</form>'
  258. document.write(formObjCar)
  259. }
  260. //Table
  261. // На тестовых данных проверено
  262. {
  263. const persons = [
  264. {
  265. name: 'Мария',
  266. fatherName: 'Ивановна',
  267. surname: 'Иванова',
  268. sex: 'female'
  269. },
  270. {
  271. name: 'Николай',
  272. fatherName: 'Иванович',
  273. surname: 'Иванов',
  274. age: 15
  275. },
  276. {
  277. name: 'Петр',
  278. fatherName: 'Иванович',
  279. surname: 'Иванов',
  280. married: true
  281. },
  282. ]
  283. let i = 0
  284. let arrKeys = []
  285. for (let x of persons){
  286. arrKeys += Object.keys(persons[i])+','
  287. i++
  288. }
  289. arrKeys = arrKeys.split(',').slice(0,-1)
  290. const makeUniq = (arr) => {
  291. return arr.filter((el1, el2) => arr.indexOf(el1) === el2);
  292. }
  293. arrKeys = makeUniq(arrKeys)
  294. let arrValue = []
  295. for (let objPersons of persons){
  296. arrValue.push(arrKeys.map(el=>objPersons[el]===undefined?objPersons[el]=' ':objPersons[el]))
  297. }
  298. arrValue.unshift(arrKeys)
  299. let arrTable = "<table border='1'>"
  300. for (let arrEl of arrValue){
  301. arrTable += '<tr>'
  302. for (let cell of arrEl){
  303. arrTable += '<td>'+cell+'</td>'
  304. }
  305. arrTable+= '</tr>'
  306. }
  307. arrTable+= "</table>"
  308. document.write(arrTable)
  309. }