How do I extract only the time range KEYS
within an array of objects?
The following is my code:
console.log(tempRemovedTimeRanges);
const removedKeys = tempRemovedTimeRanges
.flatMap(obj => Object.values(obj))
.filter(dayArray => dayArray.some(dayObj => Object.keys(dayObj)[0] === elementToInsert))
.flatMap(dayArray =>
dayArray
.filter(dayObj => Object.keys(dayObj)[0] === elementToInsert)
.flatMap(dayObj => dayObj[elementToInsert].flatMap(range => Object.keys(range)))
);
console.log(removedKeys);
The code above logs out:
(8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
[]
As you can see the console.log(removedKeys);
code logs out an empty array.
Allow me to expand tempRemovedTimeRanges
array for you to get an idea of the tempRemovedTimeRanges
structure:
(8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {Wednesdays: Array(2)}
1: {Fridays: Array(2)}
2: {Wednesdays: Array(2)}
3: {Fridays: Array(2)}
4: {Wednesdays: Array(2)}
5: {Fridays: Array(2)}
6: {Wednesdays: Array(2)}
7: {Fridays: Array(2)}
[]
As you can see, the tempRemovedTimeRanges
reveals an array of objects representing days
.
Allow me to expand the first object 0: {Wednesdays: Array(2)}
further.
Kindly note that all objects share a similar structure as the first object 0: {Wednesdays: Array(2)}
(8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0:
Wednesdays: Array(2)
0: {T_04:00_Start: 1703034000, T_08:00_End: 1703048400}
1: {T_04:00_Start: 1703638800, T_08:00_End: 1703653200}
1: {Fridays: Array(2)}
2: {Wednesdays: Array(2)}
3: {Fridays: Array(2)}
4: {Wednesdays: Array(2)}
5: {Fridays: Array(2)}
6: {Wednesdays: Array(2)}
7: {Fridays: Array(2)}
The following section represents two time ranges for the Wednesdays
object.
The first one is T_04:00_Start
to T_08:00_End
and T_04:00_Start
to T_08:00_End
{T_04:00_Start: 1703034000, T_08:00_End: 1703048400}
{T_04:00_Start: 1703638800, T_08:00_End: 1703653200}
The keys T_04:00_Start
, T_08:00_End
, T_04:00_Start
and T_08:00_End
and NOT the values (timestamps) are what I am interesting in capturing for each day in the tempRemovedTimeRanges
array
Now going back to my code:
console.log(tempRemovedTimeRanges);
const removedKeys = tempRemovedTimeRanges
.flatMap(obj => Object.values(obj))
.filter(dayArray => dayArray.some(dayObj => Object.keys(dayObj)[0] === elementToInsert))
.flatMap(dayArray =>
dayArray
.filter(dayObj => Object.keys(dayObj)[0] === elementToInsert)
.flatMap(dayObj => dayObj[elementToInsert].flatMap(range => Object.keys(range)))
);
console.log(removedKeys);
The desired output of console.log(removedKeys);
are all the time ranges keys
for each day found in the tempRemovedTimeRanges
. Those would therefore look like this:
[T_04:00_Start,
T_08:00_End,
T_04:00_Start,
T_08:00_End,
T_12:00_Start,
T_16:00_End,
T_12:00_Start,
T_16:00_End,
T_20:00_Start,
T_00:00_End,
T_20:00_Start,
T_00:00_End,
T_16:00_Start,
T_20:00_End,
T_16:00_Start,
T_20:00_End]
How do I modify my code to do this?