loading
main.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
48
// BASE EXAMPLE

function shapeTimesObjectIntoIterableArray(timesObject) {
	if (!timesObject || Object.keys(timesObject).length === 0) return [];
	
	const res = Object.keys(timesObject)
		.filter(day => timesObject[day].length)
		.reduce((allDays, day) => {

			const time = timesObject[day][0];

			const openingTime = time.open + '-' + time.close;
				
				allDays[openingTime] = allDays[openingTime] || 
				{ open: time.open, close: time.close, days: [] };

				allDays[openingTime].days.push(day);
			return allDays;
		}, {});
	
	const times = Object.keys(res).map(key => res[key])
	
	return times ;
}

const timesObject = {
	monday: [{ open: '00:00', close: '23:00' }],
	tuesday: [{ open: '00:00', close: '23:00' }],
	wednesday: [{ open: '01:00', close: '23:00' }],
	thursday: [],
	friday: [],
	saturday: [],
	sunday: [],
};

const actual = shapeTimesObjectIntoIterableArray(timesObject);

shapeTimesObjectIntoIterableArray(timesObject);

/*
 [ { open: '00:00', close: '23:00', days: [ 'monday', 'tuesday' ] } ]
 */
 

 
 
 
Native Browser JavaScript