script.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var persons = [
  2. {name: "Иван", age: 17},
  3. {name: "Мария", age: 35},
  4. {name: "Алексей", age: 73},
  5. {name: "Яков", age: 12},
  6. ]
  7. persons.sort((a,b) => a.age > b.age ? 1 : -1);
  8. sort(persons, "age");
  9. //
  10. persons.sort((a,b) => a.name > b.name ? 1 : -1);
  11. sort(persons, "name", false);
  12. //array map
  13. let arr = ["1", {}, null, undefined, "500", 700];
  14. let result = arr.map(function(item){
  15. let number = parseInt(item);
  16. return isNaN(number) ? item : number;
  17. });
  18. console.log(result);
  19. //array reduce
  20. let arr2 = ["0", 5, 3, "string", null];
  21. let arr3 = arr2.filter(item => typeof item === 'number');
  22. arr3.reduce((a, b) => a * b);
  23. console.log(number02)
  24. //object filter
  25. var phone = {
  26. brand: "meizu",
  27. model: "m2",
  28. ram: 2,
  29. color: "black",
  30. };
  31. function filter (a, b){
  32. let phone = {}
  33. for (let key in a){
  34. if (b (key, a[key])) {
  35. phone [key] = a[key];
  36. }
  37. }
  38. return phone
  39. }
  40. console.log(filter(phone,(key,value) => key == "color" || value == 2));
  41. //object map
  42. function personeMap(a, b){
  43. let filter = {}
  44. for ( let key in a){
  45. filter = {...filter, ...b([key], a[key])};
  46. }
  47. return filter
  48. }
  49. let resultat = personeMap({name: "Иван", age: 17},function(key,value){
  50. var result = {};
  51. result[key+"_"] = value + "$";
  52. return result;
  53. })
  54. console.log(resultat)