My head has been hurting for the past few hours trying to solve this issue that I have. I feel like I'm approaching it in a very unneccessarily hard way.
I'm trying to do the following:
fullRoutine is an array that looks like this:
0: {html: {…}, category: "prehab", freq: 4}
1: {html: {…}, category: "prehab", freq: 4}
2: {html: {…}, category: "prehab", freq: 4}
3: {html: {…}, category: "prehab", freq: 4}
4: {html: {…}, category: "prehab", freq: 2}
5: {html: {…}, category: "prehab", freq: 2}
6: {html: {…}, category: "prehab", freq: 2}
7: {html: {…}, category: "prehab", freq: 2}
8: {html: {…}, category: "prehab", freq: 2}
9: {html: {…}, category: "prehab", freq: 2}
10: {html: {…}, category: "skillTechnique", freq: 2}
11: {html: {…}, category: "skillTechnique", freq: 2}
12: {html: {…}, category: "skillTechnique", freq: 2}
13: {html: {…}, category: "skillTechnique", freq: 2}
14: {html: {…}, category: "skillTechnique", freq: 2}
15: {html: {…}, category: "skillTechnique", freq: 2}
16: {html: {…}, category: "skillTechnique", freq: 2}
17: {html: {…}, category: "skillTechnique", freq: 2}
18: {html: {…}, category: "upperbody_strengthPrimary", freq: 1}
19: {html: {…}, category: "upperbody_strengthPrimary", freq: 1}
20: {html: {…}, category: "upperbody_strengthPrimary", freq: 1}
21: {html: {…}, category: "upperbody_strengthPrimary", freq: 1}
22: {html: {…}, category: "upperbody_strengthPrimary", freq: 1}
23: {html: {…}, category: "lowerbody_strengthPrimary", freq: 1}
24: {html: {…}, category: "lowerbody_strengthPrimary", freq: 1}
25: {html: {…}, category: "upperbody_strengthSecondary", freq: 1}
26: {html: {…}, category: "upperbody_strengthSecondary", freq: 1}
27: {html: {…}, category: "upperbody_strengthSecondary", freq: 1}
28: {html: {…}, category: "upperbody_strengthSecondary", freq: 1}
29: {html: {…}, category: "upperbody_strengthSecondary", freq: 1}
30: {html: {…}, category: "lowerbody_strengthSecondary", freq: 1}
31: {html: {…}, category: "lowerbody_strengthSecondary", freq: 1}
32: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
33: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
34: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
35: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
36: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
37: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
38: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
39: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
40: {html: {…}, category: "lowerbody_strengthIsolation", freq: 1}
41: {html: {…}, category: "lowerbody_strengthIsolation", freq: 1}
42: {html: {…}, category: "lowerbody_strengthIsolation", freq: 2}
43: {html: {…}, category: "lowerbody_strengthIsolation", freq: 2}
44: {html: {…}, category: "mobility", freq: 2}
45: {html: {…}, category: "mobility", freq: 2}
46: {html: {…}, category: "mobility", freq: 2}
47: {html: {…}, category: "mobility", freq: 2}
This is the Max object:
const max = {
prehab: 2,
skillTechnique: 2,
upperbody_strengthPrimary: 2,
lowerbody_strengthPrimary: 2,
upperbody_strengthSecondary: 2,
lowerbody_strengthSecondary: 2,
upperbody_strengthIsolation: 2,
lowerbody_strengthIsolation: 2,
mobility: 1
};
This is the dailyFrequency function:
function dailyFrequency(day) {
const usesDaily = {};
for (const { category } of day) {
usesDaily[category] = (usesDaily[category] || 0) + 1;
}
// console.log("Daily frequency measurer:");
// console.log(usesDaily);
return usesDaily;
}
My idea is pretty simple. I want to parse the fullRoutine array into multiple smaller arrays. The max object contains a category max for each array item. For example. Items in the fullRoutine array with the category "prehab" are only allowed to be parsed 2 times in the newly created array. After that, a new array must be made for the remainder two.
Don't get confused by the freq property in the fullRoutine array. That has nothing to do with any of this.
I used the dailyFrequency function to measure the frequency that is in the fullRoutine array now. My solution to this problem led me nowhere:
const contains = [];
for (let i = 0; i < fullRoutine.length; i++) {
for (let freqKey in dailyFrequency(contains)) {
for (let maxKey in max) {
if (fullRoutine[i].category === freqKey) {
console.log("yes?");
if (freqKey === maxKey) {
if (max[maxKey] < dailyFrequency(contains)[freqKey]) {
contains.push(fullRoutine[i]);
}
}
}
}
}
}
I feel like there is a much easier way to approach this without bursting my head. Thank you in advance.
EDIT: The first small Array that I am trying to get out of this is:
0: {html: {…}, category: "prehab", freq: 4}
1: {html: {…}, category: "prehab", freq: 4}
2: {html: {…}, category: "skillTechnique", freq: 2}
3: {html: {…}, category: "skillTechnique", freq: 2}
4: {html: {…}, category: "upperbody_strengthPrimary", freq: 1}
5: {html: {…}, category: "upperbody_strengthPrimary", freq: 1}
6: {html: {…}, category: "lowerbody_strengthPrimary", freq: 1}
7: {html: {…}, category: "lowerbody_strengthPrimary", freq: 1}
8: {html: {…}, category: "upperbody_strengthSecondary", freq: 1}
9: {html: {…}, category: "upperbody_strengthSecondary", freq: 1}
10: {html: {…}, category: "lowerbody_strengthSecondary", freq: 1}
11: {html: {…}, category: "lowerbody_strengthSecondary", freq: 1}
12: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
13: {html: {…}, category: "upperbody_strengthIsolation", freq: 2}
14: {html: {…}, category: "lowerbody_strengthIsolation", freq: 1}
15: {html: {…}, category: "lowerbody_strengthIsolation", freq: 1}
16: {html: {…}, category: "mobility", freq: 2}