Hasli4

JS. Objects

Jul 15th, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Задача 1. Профиль пользователя
  2. let profile = {
  3.   firstName: 'Иван',
  4.   lastName: 'Иванов',
  5.   age: 30
  6. };
  7. profile.fullName = profile.firstName + ' ' + profile.lastName;
  8. console.log(profile.fullName, profile.age);
  9. // Иван Иванов 30
  10.  
  11. // Задача 2. Корзина товаров
  12. let cart = { items: [], total: 0 };
  13. function addItem(name, price) {
  14.   cart.items.push({ name: name, price: price });
  15.   cart.total += price;
  16. }
  17. addItem('Apple', 50);
  18. addItem('Banana', 30);
  19. console.log(cart.items, cart.total);
  20. // [{name:'Apple',price:50},{name:'Banana',price:30}] 80
  21.  
  22. // Задача 3. Удаление неактивных
  23. let users = {
  24.   user1: { active: true },
  25.   user2: { active: false },
  26.   user3: { active: false },
  27.   user4: { active: true }
  28. };
  29. function removeInactive(userObj) {
  30.   for (let key in userObj) {
  31.     if (userObj[key].active === false) {
  32.       delete userObj[key];
  33.     }
  34.   }
  35. }
  36. removeInactive(users);
  37. console.log(users);
  38. // { user1: {active:true}, user4: {active:true} }
  39.  
  40. // Задача 4. Подсчёт свойств
  41. function countProps(obj) {
  42.   return Object.keys(obj).length;
  43. }
  44. console.log(countProps({a:1, b:2, c:3})); // 3
  45. console.log(countProps({}));               // 0
  46.  
  47. // Задача 5. Копия объекта
  48. let orig = { a: 1, b: 2 };
  49. let copy = {};
  50. for (let key in orig) {
  51.   if (orig.hasOwnProperty(key)) {
  52.     copy[key] = orig[key];
  53.   }
  54. }
  55. copy.a = 10;
  56. console.log(orig.a, copy.a); // 1 10
  57.  
  58. // Задача 6. Перебор с фильтром
  59. let book = { title: 'JS Guide', author: 'Author', year: 2020, pages: 300 };
  60. for (let key in book) {
  61.   if (typeof book[key] === 'string') {
  62.     console.log(key, book[key]);
  63.   }
  64. }
  65. // title JS Guide
  66. // author Author
  67.  
  68. // Задача 7. Обратный ключ–значение
  69. let map = { a: 1, b: 2, c: 3 };
  70. let inv = {};
  71. for (let key in map) {
  72.   inv[map[key]] = key;
  73. }
  74. console.log(inv); // { '1':'a', '2':'b', '3':'c' }
  75.  
  76. // Задача 8. Метод в объекте
  77. let rectangle = {
  78.   width: 10,
  79.   height: 5,
  80.   area: function() {
  81.     return this.width * this.height;
  82.   }
  83. };
  84. console.log(rectangle.area()); // 50
  85.  
  86. // Задача 9. Глубокое копирование
  87. let settings = { volume: 10, options: { theme: 'dark' } };
  88. let copySettings = JSON.parse(JSON.stringify(settings));
  89. copySettings.options.theme = 'light';
  90. console.log(settings.options.theme, copySettings.options.theme);
  91. // 'dark', 'light'
  92.  
  93. // Задача 10. Объединение объектов
  94. let defaults = { theme: 'light', show: true };
  95. let userPrefs = { theme: 'dark' };
  96. let config = Object.assign({}, defaults, userPrefs);
  97. console.log(config.theme, config.show);
  98. // 'dark', true
  99.  
Advertisement
Add Comment
Please, Sign In to add comment