main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // 3 persons,different fields and fields check
  2. let a = {
  3. name: 'Petro',
  4. surname: 'Petrovich',
  5. "age": '30',
  6. "fathername": 'Petroff',
  7. "sex": 'male',
  8. }
  9. let b = {
  10. name: 'Ivan',
  11. surname: 'Ivanov',
  12. "age": '35',
  13. "fathername": 'Ivanovich',
  14. "sex": 'male',
  15. }
  16. let c = {
  17. name: 'Sveta',
  18. surname: 'Svetova',
  19. "age": '40',
  20. "fathername": 'Serhiivna',
  21. "sex": 'female',
  22. }
  23. if ("age" in a) {
  24. alert(a.age);
  25. }
  26. if ("fathername" in a) {
  27. alert(a.fathername);
  28. }
  29. if ("sex" in a) {
  30. alert(a.sex)
  31. }
  32. if ("age" in b) {
  33. alert(b.age);
  34. }
  35. if ("fathername" in b) {
  36. alert(b.fathername);
  37. }
  38. if ("sex" in b) {
  39. alert(b.sex)
  40. }
  41. if ("age" in c) {
  42. alert(c.age);
  43. }
  44. if ("fathername" in c) {
  45. alert(c.fathername);
  46. }
  47. if ("sex" in c) {
  48. alert(c.sex);
  49. }
  50. // array of persons
  51. let persons = [a, b, c, { name: 'Olha', surname: 'Petrova', age: '45', sex: 'female' }];
  52. // loop of persons
  53. let output;
  54. for (output = 0; output < persons.length; ++output) {
  55. console.log(persons[output])
  56. }
  57. // loop of name and surname
  58. let outputNameAndSurname;
  59. for (outputNameAndSurname = 0; outputNameAndSurname < persons.length; ++outputNameAndSurname) {
  60. console.log(persons[outputNameAndSurname].name + " " + persons[outputNameAndSurname].surname);
  61. };
  62. // loop of loop of values
  63. for (let output = 0; output < persons.length; output++) {
  64. for (let key in persons[output]) {
  65. console.log(persons[output][key]);
  66. }
  67. };
  68. // fullName
  69. for (let key of persons) {
  70. if (key.name && key.surname) {
  71. key.fullName = key.name + " " + key.surname;
  72. }
  73. }
  74. console.log(persons);
  75. // serialize
  76. JSON.stringify(arrofPersons);
  77. // deserialize
  78. let d = '{"name" : "Olha","surname" : "Petrova" ,"age" : 45,"sex":"female"}'
  79. d = JSON.parse(d)
  80. persons[3] = d
  81. console.log(persons)
  82. // HTML
  83. let str = "<table border='1'>";
  84. for (let i = 0; i < 4; i++) {
  85. for (let key in persons[i]) {
  86. str += `<tr><td>${[key]}</td><td>${persons[i][key]}</td></tr>`;
  87. }
  88. }
  89. str += "</table>";
  90. console.log(str);
  91. document.write(str);
  92. // HTML optional fields
  93. let strTwo = "<table border='1'>"
  94. for (let i = 0; i < 4; i++) {
  95. for (let key in persons[i]) {
  96. strTwo += `<tr><td>${key}</td><td>${persons[i][key]}</td></tr>`
  97. }
  98. } strTwo += "</table>"
  99. console.log(strTwo)
  100. document.write(strTwo)
  101. // HTML tr color
  102. let strThree = "<table border='1'>";
  103. for (let i = 0; i < 4; i++) {
  104. for (let key in persons[i]) {
  105. strThree += `<tr style="color: blue;><td>${[key]}</td><td>${persons[i][key]
  106. }</td></tr>`;
  107. }
  108. }
  109. strThree += "</table>";
  110. console.log(strThree);
  111. document.write(strThree);
  112. // HTML th optional
  113. // destruct array
  114. {
  115. let arr = [1, 2, 3, 4, 5, "a", "b", "c"]
  116. let [odd1, even1, odd2, even2, odd3, ...letters] = arr
  117. //destruct string
  118. let arr2 = [1, "abc"]
  119. let [number, [s1, s2, s3]] = arr2
  120. // destruct 2
  121. let obj = {
  122. name: 'Ivan',
  123. surname: 'Petrov',
  124. children: [{ name: 'Maria' }, { name: 'Nikolay' }]
  125. }
  126. let {
  127. children: [{ name: name1 }, { name: name2 }] } = obj;
  128. //destruct 3
  129. let numberTwo = [1, 2, 3, 4, 5, 6, 7, 10];
  130. let [a, b, { length = numberTwo.length }] = numberTwo;
  131. }