index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* 3 persons */
  2. let a = {
  3. name: 'Andriy',
  4. surname: 'Yarmolenko'
  5. }
  6. let b = {
  7. name: 'Oleksandr',
  8. surname: 'Zinchenko'
  9. }
  10. let c = {
  11. name: 'Vitaliy',
  12. surname: 'Mykolenko'
  13. }
  14. /* different fields */
  15. a.age = '32',
  16. a.nationality = 'Ukrainian',
  17. a.club = 'West Ham United'
  18. b.age = '25',
  19. b.nationality = 'Ukrainian',
  20. b.club = 'Manchester City'
  21. c.age = '23',
  22. c.nationality = 'Ukrainian',
  23. c.club = 'Everton'
  24. /* fields check */
  25. if ('nationality' in a, b, c) {
  26. alert(a.surname + ' ' + a.name + ' ' + a.nationality + '\n' + b.surname + ' ' + b.name + ' ' + b.nationality + '\n' + c.surname + ' ' + c.name + ' ' + c.nationality)
  27. }
  28. /* array of persons */
  29. let persons = []
  30. persons.push(a, b, c)
  31. persons.push({name: 'Ruslan', surname: 'Malynovskiy', age: '29', nationality: 'Ukrainian', club: 'Atalanta'})
  32. /* loop of persons */
  33. for (let i = 0; i < persons.length; i++) {
  34. console.log(persons[i])
  35. }
  36. /* loop of name and surname */
  37. for (let i = 0; i < persons.length; i++) {
  38. console.log(persons[i].name + ' ' + persons[i].surname)
  39. }
  40. /* loop of loop of values */
  41. for (let i = 0; i < persons.length; i++) {
  42. for (let key in persons[i]) {
  43. console.log(persons[i][key])
  44. }
  45. }
  46. /* fullName */
  47. for (let newObject of persons) {
  48. if (newObject.name && newObject.surname) {
  49. newObject.fullName = newObject.name + ' ' + newObject.surname
  50. }
  51. }
  52. /* serialize */
  53. JSON.stringify(persons)
  54. /* deserialize */
  55. let d = JSON.parse('{"name" : "Andriy", "surname" : "Lunin", "age" : "23", "nationality" : "Ukrainian", "club" : "Real Madrid"}')
  56. persons.push(d)
  57. /* HTML */
  58. var str = "<table border='1'>"
  59. for (let i = 0; i < persons.length; i++) {
  60. str += `<tr><td>${persons[i].name}</td><td>${persons[i].surname}</td></tr>`
  61. }
  62. str += "</table>"
  63. console.log(str)
  64. document.write(str)
  65. /* HTML optional fields */
  66. var str = "<table border='1'>"
  67. for (let i = 0; i < persons.length; i++) {
  68. for (let key in persons[i]) {
  69. str += `<tr><td>${key}</td><td>${persons[i][key]}</td></tr>`
  70. }
  71. }str += "</table>"
  72. console.log(str)
  73. document.write(str)
  74. /* HTML tr color */
  75. var str = "<table border='1'>"
  76. for (let i = 0; i < persons.length; i++) {
  77. for (let key in persons[i]) {
  78. str += `<tr style = "color : blue;"><td>${key}</td><td>${persons[i][key]}</td></tr>`
  79. }
  80. }str += "</table>"
  81. console.log(str)
  82. document.write(str)
  83. /* destruct array */
  84. let arr = [1,2,3,4,5, "a", "b", "c"]
  85. let [odd1, even1, odd2, even2, odd3, ...abc] = arr
  86. /* destruct string */
  87. let arr2 = [1, "abc"]
  88. let [number, [s1, s2, s3]] = arr2
  89. /* destruct 2 */
  90. let obj = {name: 'Ivan',
  91. surname: 'Petrov',
  92. children: [{name: 'Maria'}, {name: 'Nikolay'}]}
  93. let {children: [{name: name1}, {name: name2}]} = obj
  94. /* destruct 3 */
  95. let arr3 = [1, 2, 3, 4, 5, 6, 7, 10];
  96. let {0:a, 1:b, length} = arr3