script.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //-----------------------------------------------------------sort---------------------------------------------------------------------------
  2. var persons = [{
  3. name: "Иван",
  4. age: 17
  5. },
  6. {
  7. name: "Мария",
  8. age: 35
  9. },
  10. {
  11. name: "Алексей",
  12. age: 73
  13. },
  14. {
  15. name: "Яков",
  16. age: 12
  17. },
  18. ]
  19. function sort(array, key, increase = true) {
  20. if (increase) {
  21. array.sort(function (a, b) {
  22. if (a[key] > b[key]) {
  23. return 1;
  24. }
  25. if (a[key] < b[key]) {
  26. return -1;
  27. }
  28. return 0;
  29. });
  30. } else {
  31. array.sort(function (a, b) {
  32. if (a[key] < b[key]) {
  33. return 1;
  34. }
  35. if (a[key] > b[key]) {
  36. return -1;
  37. }
  38. return 0;
  39. });
  40. }
  41. return array;
  42. };
  43. // console.log(sort(persons, "age")); //сортирует по возрасту по возрастанию
  44. // console.log(sort(persons, "name"));
  45. // console.log(sort(persons, "age", true));
  46. // console.log(sort(persons, "name", true));
  47. // console.log(sort(persons, "name", false)); //сортирует по имени по убыванию
  48. // console.log(sort(persons, "age", false)); //сортирует по имени по убыванию
  49. //-----------------------------------------------------------array map----------------------------------------------------------------------
  50. let arrayMap = ["1", {}, null, undefined, "500", 700];
  51. let arrayMap2 = arrayMap.map((item) => {
  52. if (typeof item == "string") return isNaN(parseInt(item, 10)) ? item : parseInt(item, 10);
  53. return item;
  54. });
  55. // console.log(arrayMap2);
  56. //-----------------------------------------------------------array reduce--------------------------------------------------------------------
  57. let arrayReduce = ["0", 5, 3, "string", null];
  58. let arrayPow = arrayReduce.reduce((a, b) => (typeof b == "number" ? a * b : a), 1);
  59. console.log(arrayPow);
  60. //-----------------------------------------------------------object filter--------------------------------------------------------------------
  61. var phone = {
  62. brand: "meizu",
  63. model: "m2",
  64. ram: 2,
  65. color: "black",
  66. };
  67. function filter(obj) {
  68. let obj2 = {};
  69. for (let key in obj) {
  70. if (key == "color" || obj[key] == 2) {
  71. obj2[key] = obj[key];
  72. }
  73. }
  74. return obj2;
  75. }
  76. console.log(filter(phone));
  77. //-----------------------------------------------------------object map--------------------------------------------------------------------
  78. let someObject = {
  79. name: "Иван",
  80. age: 17
  81. };
  82. function map(obj) {
  83. let temp = {};
  84. for (let i in obj) {
  85. temp[i + '_'] = i + '$';
  86. }
  87. return temp;
  88. }
  89. console.log(map(someObject));
  90. //-----------------------------------------------------------recursion--------------------------------------------------------------------
  91. function arithmeticProgression(num, increment, rows) {
  92. if (rows <= 1) {
  93. return num;
  94. }
  95. return num + arithmeticProgression(num + increment, increment, rows - 1)
  96. }
  97. console.log(arithmeticProgression(1, 4, 8));
  98. //-----------------------------------------------------------HTML Tree--------------------------------------------------------------------
  99. var someTree = {
  100. tagName: "table", //html tag
  101. children: [ //вложенные тэги
  102. {
  103. tagName: "tr",
  104. children: [{
  105. tagName: "td",
  106. text: "some text",
  107. },
  108. {
  109. tagName: "td",
  110. text: "some text 2",
  111. }
  112. ]
  113. }
  114. ],
  115. attrs: {
  116. border: 1,
  117. },
  118. }
  119. function construct(obj) {
  120. let node = document.createElement(obj.tagName);
  121. if ("attrs" in obj) {
  122. for (let attrName in obj.attrs) {
  123. node.setAttribute(attrName, obj.attrs[attrName])
  124. }
  125. }
  126. if ("children" in obj) {
  127. for (let child in obj.children) {
  128. node.append(construct(obj.children[child]));
  129. }
  130. }
  131. if ("text" in obj) {
  132. node.innerText = obj.text;
  133. }
  134. return node;
  135. }
  136. document.body.append(construct(someTree));
  137. console.log(construct(someTree));
  138. if () {
  139. if () {
  140. dbvbskdnvn
  141. }
  142. }