script.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ////Sort
  2. var 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. let middleArr = [];
  10. for (let i = 0; i < arr.length; i++) {
  11. middleArr.push(arr[i]);
  12. }
  13. for (let j = 0; j < middleArr.length; j++) {
  14. let index = 0
  15. for (let k = 1; k < middleArr.length;) {
  16. if (middleArr[index][key] < middleArr[k][key]) {
  17. [middleArr[index], middleArr[k]] = [middleArr[k], middleArr[index]]
  18. k += 2
  19. index += 1
  20. } else {
  21. k += 1
  22. index += 1
  23. }
  24. }
  25. }
  26. if (boolean == true) {
  27. middleArr.reverse()
  28. }
  29. return middleArr
  30. }
  31. console.log(sort(persons, "age"));
  32. console.log(sort(persons, "name", false))
  33. ///////Array map
  34. let array = ["1", {}, null, undefined, "500", 700]
  35. let newArray = array.map(function (num) {
  36. if (!num === false && !isNaN(+num)) {
  37. return +num
  38. }
  39. return num
  40. })
  41. //console.log(newArray)
  42. //Array reduce
  43. let reduceArray = ["0", 5, 3, "string", null]
  44. let result = reduceArray.reduce(function(a, b){
  45. if(typeof a !== "number"){
  46. a = b
  47. return a
  48. } else if
  49. (typeof b !== "number"){
  50. return a
  51. } else {
  52. a = a*b
  53. }
  54. return a
  55. })
  56. console.log(result)