script.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. for(let i = 0; i < obj.length; i++) {
  73. let value = "";
  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. }
  83. // fullName(persons)
  84. var json = (JSON.stringify(persons, null, " "))
  85. // console.log(json)
  86. var e = JSON.parse('{"name":"Petr","surname":"Vlasenco","fathername":"Andreevich","age":"40","country":"Belarus","marriage":"true"}')
  87. persons.push(e)
  88. function toHtmlTable(obj) {
  89. var str = "<table border='1'>"
  90. for (let i=0;i<obj.length;i++){
  91. let check = obj[i]
  92. str += `<tr><td>${i}</td>`
  93. for (let key in check){
  94. str += `<td>${check[key]}</td>`
  95. }
  96. str += `</tr>`
  97. }
  98. str += "</table>"
  99. document.write(str)
  100. }
  101. // toHtmlTable(persons)
  102. function htmlOptional(obj) {
  103. debugger
  104. let str = `<table border='1'><tr><td style = "background-color: yellow"></td>`
  105. let valueMas =[]
  106. for (let i=0;i<obj.length;i++){
  107. let check = obj[i]
  108. for(let key in check) {
  109. if(str.includes(key)){
  110. } else{
  111. str +=`<th style = "background-color: yellow">${key}</th>`
  112. valueMas.push(key)
  113. }
  114. }
  115. }
  116. for (let j=0;j<obj.length;j++){
  117. let check = obj[j]
  118. str += `<tr><td style = "background-color: yellow">${j}</td>`
  119. let keysArr = Object.keys(check);
  120. for(let k = 0; k < valueMas.length; k++) {
  121. if(keysArr.indexOf(valueMas[k]) == -1) {
  122. str += `<td style = "background-color: red"></td>`
  123. } else {
  124. str += `<td style = "background-color: #89e0c0">${check[valueMas[k]]}</td>`
  125. }
  126. }
  127. str += `</tr>`
  128. }
  129. str += "</table>"
  130. console.log(str)
  131. document.write(str)
  132. }
  133. htmlOptional(persons)