-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform-practice.js
47 lines (40 loc) · 1.12 KB
/
transform-practice.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
function formatSeatData(data) {
const seatsByRoom = {};
data.map(ticket => {
const { room, table, seat } = ticket;
if (!seatsByRoom[room]) {
seatsByRoom[room] = {};
}
if (!seatsByRoom[room][table]) {
seatsByRoom[room][table] = [];
}
seatsByRoom[room][table].push(seat);
});
const formattedSeats = [];
Object.keys(seatsByRoom).map(rooms =>
Object.keys(rooms)
.map(seats => seatsByRoom[rooms])
.map(tables => {
for (let [table, seats] of Object.entries(tables)) {
formattedSeats.push(
`Room ${rooms}, Table ${table}, ${
seats.length > 1 ? "Seats" : "Seat"
} ${seats.join(", ")}`
);
}
})
);
return formattedSeats;
}
const data = [
{ room: "A", table: "B", seat: 2 },
{ room: "A", table: "B", seat: 6 },
{ room: "A", table: "B", seat: 10 },
{ room: "B", table: "B", seat: 2 },
{ room: "B", table: "C", seat: 3 }
];
console.log(formatSeatData(data));
// Output should be formatted as:
// ['room A, Table B, Seats, 2, 6, 10',
// 'room B, Table B, Seat 2',
// 'room B', Table B, Seat 3']