script.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. let mas = []
  2. let a = {
  3. name: "Misha",
  4. surname: "Muslim"
  5. }
  6. a.fathername = "Svinonich"
  7. a.age = "24"
  8. a.country = "Ukraine"
  9. a.sex = "male"
  10. a.marriage = true;
  11. let b = {
  12. name: "Oleh",
  13. surname: "Glamazda",
  14. }
  15. b.age = "25"
  16. b.country = "Ukraine"
  17. b.marriage = false
  18. let c = {
  19. name: "Katya",
  20. surname: "Krasnoshok",
  21. }
  22. c.age = "30"
  23. c.sex = "female"
  24. let persons = []
  25. persons.push(a, b, c)
  26. function fieldsCheck(check) {
  27. for(let i = 0; i < persons.length; i++) {
  28. let str = ""
  29. check = persons[i]
  30. for(let key in check){
  31. (key == "name" || key == "surname" ) ? str += ""
  32. : str += key + " "
  33. }
  34. if(str == "") {
  35. alert("Nothing in " + check.name)
  36. } else {
  37. alert(str + " in " + check.name)
  38. }
  39. }
  40. }
  41. // fieldsCheck()
  42. persons.push({name: "Tom",
  43. surname: "Motgan",
  44. country: "UK",
  45. marriage: true
  46. })
  47. function personsObj(obj) {
  48. for (let i = 0; i < obj.length; i++){
  49. console.log(obj[i])
  50. }
  51. }
  52. // personsObj(persons)
  53. function personsNamSurn(obj) {
  54. for(let i = 0; i < obj.length; i++) {
  55. console.log(obj[i].name + " " + obj[i].surname)
  56. }
  57. }
  58. // personsNamSurn(persons)
  59. function personsValues(obj) {
  60. let cons = "";
  61. for(let i = 0; i < obj.length; i++) {
  62. let check = obj[i]
  63. for (let key in check) {
  64. cons += check[key] + " "
  65. }
  66. cons += "\n"
  67. }
  68. console.log(cons)
  69. }
  70. // personsValues(persons)
  71. function fullName(obj) {
  72. let value = "";
  73. for(let i = 0; i < obj.length; i++) {
  74. let check = obj[i]
  75. for(let key in check) {
  76. if(key == "name" || key == "fathername" || key == "surname"){
  77. value += check[key] + " "
  78. }
  79. }
  80. check.fullName = value
  81. }
  82. console.log(value)
  83. }
  84. fullName(persons)
  85. var json = (JSON.stringify(persons, null, " "))
  86. // console.log(json)
  87. var e = JSON.parse('{"name":"Petr","surname":"Vlasenco","fathername":"Andreevich","age":"40","country":"Belarus","marriage":"true"}')
  88. persons.push(e)
  89. function toHtmlTable(obj) {
  90. var str = "<table border='1'>"
  91. for (let i=0;i<obj.length;i++){
  92. let check = obj[i]
  93. str += `<tr><td>${i}</td>`
  94. for (let key in check){
  95. str += `<td>${check[key]}</td>`
  96. }
  97. str += `</tr>`
  98. }
  99. str += "</table>"
  100. document.write(str)
  101. }
  102. // toHtmlTable(persons)
  103. function htmlOptional(obj) {
  104. debugger
  105. let str = `<table border='1'><tr><td style = "background-color: yellow"></td>`
  106. let valueMas =[]
  107. for (let i=0;i<obj.length;i++){
  108. let check = obj[i]
  109. for(let key in check) {
  110. if(str.includes(key)){
  111. } else{
  112. str +=`<th style = "background-color: yellow">${key}</th>`
  113. valueMas.push(key)
  114. }
  115. }
  116. }
  117. for (let j=0;j<obj.length;j++){
  118. let check = obj[j]
  119. str += `<tr><td style = "background-color: yellow">${j}</td>`
  120. let keysArr = Object.keys(check);
  121. for(let k = 0; k < valueMas.length; k++) {
  122. if(keysArr.indexOf(valueMas[k]) == -1) {
  123. str += `<td style = "background-color: red"></td>`
  124. } else {
  125. str += `<td style = "background-color: #89e0c0">${check[valueMas[k]]}</td>`
  126. }
  127. }
  128. str += `</tr>`
  129. }
  130. str += "</table>"
  131. console.log(str)
  132. document.write(str)
  133. }
  134. // htmlOptional(persons)