main.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //1.sort
  2. // let persons = [
  3. // {name: "Иван", age: 17},
  4. // {name: "Мария", age: 35},
  5. // {name: "Алексей", age: 73},
  6. // {name: "Яков", age: 12},
  7. // ];
  8. // function sort (arr, key, boolean = true) {
  9. // if(boolean) {
  10. // arr.sort(((a, b) => a[key] > b[key] ? 1 : -1));
  11. // }
  12. // else{
  13. // arr.sort(((a, b) => a[key] < b[key] ? 1 : -1));
  14. // }
  15. // }
  16. // sort(persons, "age");
  17. // sort(persons, "name", false);
  18. // console.log(persons);
  19. //2.array map
  20. // let arr = ["1", {}, null, undefined, "500", 700];
  21. // console.log(arr.map(item => isNaN(item) ? item : parseInt(item) || null));
  22. //3.array reduce
  23. // let arr = ["0", 5, 3, "string", null];
  24. // let reduce = arr.reduce (function(a, b) {
  25. // return((typeof(b) === 'number') ? (a *= b) : a);
  26. // }, 1)
  27. // console.log(reduce);
  28. //4.object filter
  29. // let phone = {
  30. // brand: "meizu",
  31. // model: "m2",
  32. // ram: 2,
  33. // color: "black",
  34. // };
  35. // function filter (obj, f){
  36. // for(let key in obj){
  37. // if(!f(key, obj[key])){
  38. // delete obj[key];
  39. // }
  40. // }
  41. // return obj;
  42. // }
  43. // filter(phone, (key, value) => key == "color" || value == 2);
  44. // console.log(phone);
  45. //5.object map
  46. // function map (obj, f){
  47. // let res = {};
  48. // for(let key in obj){
  49. // res[key] = obj[key];
  50. // }
  51. // return res;
  52. // }
  53. // let objMap = map({name: "Иван", age: 17},function(key,value){
  54. // let result = {};
  55. // result[key+"_"] = value + "$";
  56. // return result;
  57. // });
  58. // console.log(objMap);
  59. //6.Рекурсия
  60. //Sum
  61. // function sum (num){
  62. // if(num === 0){
  63. // return 0;
  64. // }
  65. // else{
  66. // return num + sum(num-1);
  67. // }
  68. // }
  69. // console.log(sum(3));
  70. //HTML Tree
  71. var someTree = {
  72. tagName: "table", //html tag
  73. subTags: [ //вложенные тэги
  74. {
  75. tagName: "tr",
  76. subTags: [
  77. {
  78. tagName: "td",
  79. text: "some text",
  80. },
  81. {
  82. tagName: "td",
  83. text: "some text 2",
  84. }
  85. ]
  86. }
  87. ],
  88. attrs:
  89. {
  90. border: 1,
  91. },
  92. };
  93. function a (obj){
  94. let res = '';
  95. for(let elem in obj){
  96. let el = obj[elem];
  97. if('subTags' in el){
  98. res += `<${el['tagName']}> ${a(el['subTags'])} </${el['tagName']}>`;
  99. }
  100. else{
  101. res += `<${el['tagName']}> ${el['text']} </${el['tagName']}>`;
  102. }
  103. }
  104. return res;
  105. }
  106. let str = '';
  107. if('tagName' in someTree){
  108. str += `<${someTree.tagName} border='1'>`;
  109. }
  110. str += a(someTree['subTags']);
  111. str += `</${someTree.tagName}>`;
  112. console.log(str);
  113. document.write(str);