JS_HOME_WORK/48-reduce-to-object/start.js
2025-02-05 08:47:22 +01:00

43 lines
989 B
JavaScript
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** ЗАДАЧА 48 - Использование метода "reduce" для создания объекта
*
* 1. Создайте функцию "quantitiesByCategories" с одним параметром "products"
*
* 2. Эта функция должна возвращать объект с ключами,
* равными категориям, и значениями,
* равными сумме всех количеств в каждой категории
*/
const inputProducts = [
{
title: 'Phone case',
price: 23,
quantity: 2,
category: 'Accessories',
},
{
title: 'Android phone',
price: 150,
quantity: 1,
category: 'Phones',
},
{
title: 'Headphones',
price: 78,
quantity: 1,
category: 'Accessories',
},
{
title: 'Sport Watch',
price: 55,
quantity: 2,
category: 'Watches',
},
]
console.log(quantitiesByCategories(inputProducts))
/* {
Accessories: 3,
Phones: 1,
Watches: 2
} */