index.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. function arrSort() {
  11. let persons = [
  12. {name: "Иван", age: 17},
  13. {name: "Мария", age: 35},
  14. {name: "Алексей", age: 73},
  15. {name: "Яков", age: 12},
  16. ]
  17. function personSort() {
  18. persons.sort(function(a, b){
  19. return b.age - a.age
  20. })
  21. persons.sort(function(a, b){
  22. let nameA = a.name.toLowerCase();
  23. let nameB = b.name.toLowerCase();
  24. if (nameA > nameB)
  25. return -1
  26. if (nameA < nameB)
  27. return 1
  28. })
  29. }
  30. }
  31. //=====================================================================================================================//
  32. //=============================================== array map ===========================================================//
  33. let qwerty = ["1", {}, null, undefined, "500", 700].map((qwerty) => parseInt(qwerty) ? parseInt(qwerty) : qwerty);
  34. //=====================================================================================================================//
  35. //=============================================== array reduce ========================================================//
  36. let myArr = ["0", 5, 3, "string", null].reduce((a,myArr) => (!isNaN(myArr) && typeof myArr === "number" ? a * myArr : a), 1)
  37. //=====================================================================================================================//
  38. //=============================================== object filter =======================================================//
  39. var phone = {
  40. brand: "meizu",
  41. model: "m2",
  42. ram: 2,
  43. color: "black",
  44. };
  45. function filter(arr, func) {
  46. for (let k in arr) {
  47. if (!func(k, arr[k])) {
  48. delete arr[k]
  49. }
  50. }
  51. return arr;
  52. }
  53. console.log(filter(phone, (key, value) => key == "color" || value == 2));
  54. //=====================================================================================================================//
  55. //=============================================== object map ==========================================================//
  56. const map = function (obj, func) {
  57. let objNew = {};
  58. for (let key in obj) {
  59. objNew = { ...objNew, ...func(key, obj[key]) };
  60. }
  61. return objNew;
  62. };
  63. let testObj = map({ name: "Иван", age: 17 }, function (key, value) {
  64. var result = {};
  65. result[key + "_"] = value + "$";
  66. return result;
  67. });
  68. </script>
  69. </body>
  70. </html>