-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathass10.html
More file actions
34 lines (26 loc) · 1.06 KB
/
ass10.html
File metadata and controls
34 lines (26 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
function groupByCategory(items, categoryKey) {
return items.reduce((acc, item) => {
const category = item[categoryKey];
if (!acc[category]) {
acc[category] = [];
}
acc[category].push(item);
return acc;
}, {});
}
const products = [
{ name: 'Laptop', category: 'Electronics', price: 1200 },
{ name: 'T-Shirt', category: 'Apparel', price: 25 },
{ name: 'Mouse', category: 'Electronics', price: 30 },
{ name: 'Jeans', category: 'Apparel', price: 70 }
];
const groupedProducts = groupByCategory(products, 'category');
console.log(groupedProducts);
</script>
</body>
</html>