2

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}
7
  • 2
    This is also hurting my head... I don't understand where you're going... Commented Dec 28, 2019 at 16:12
  • 2
    Can you explain why you want to parse the fullRoutine array into multiple smaller arrays? Commented Dec 28, 2019 at 16:16
  • This fullRoutine array is data for an entire week of training. My goal is to turn it into days of training. Thus into multiple smaller arrays. I hope that makes sense... Commented Dec 28, 2019 at 16:22
  • smaller arrays of what ?? Commented Dec 28, 2019 at 16:28
  • Edited original post to reflect how the smaller arrays should look like Commented Dec 28, 2019 at 16:31

3 Answers 3

1

Ok, here's my second answer that pours multiple cups of water.

Note that there are empty arrays because there is no more water to pour. You can filter them out easily.

var fullRoutine = [{
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "lowerbody_strengthPrimary",
}, {
  category: "lowerbody_strengthPrimary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "lowerbody_strengthSecondary",
}, {
  category: "lowerbody_strengthSecondary",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "lowerbody_strengthIsolation",
}, {
  category: "lowerbody_strengthIsolation",
}, {
  category: "lowerbody_strengthIsolation",
}, {
  category: "lowerbody_strengthIsolation",
}, {
  category: "mobility",
}, {
  category: "mobility",
}, {
  category: "mobility",
}, {
  category: "mobility",
}];

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
};

var newarrs = [];

var i = 0;

while (fullRoutine.length) { //if the bottle still has water
  newarrs.push([]); //get a new cup
  Object.entries(max).forEach(e => { //pour different waters
    newarrs[i].push(//add water to cup
      fullRoutine.filter(o => o.category === e[0])
                 .slice(0,e[1])
    );
    fullRoutine = //remove water from bottle
    fullRoutine.filter(o => o.category === e[0])
               .slice(e[1])
               .concat(fullRoutine
               .filter(o => o.category !== e[0]));
  });
  i++; //get ready for another cup
}

console.log(newarrs);

Basically, after it pours the water, the bottle loses some water like it's supposed to.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know how you managed to pull through... but you did. I'm amazed man. Thank you so much for your help. I need to get some sleep before I figure out how you made that work! Thanks!
1

Use .filter() to filter though the objects and .slice() to slice the first n.

var fullRoutine = [{
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "prehab",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "skillTechnique",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "upperbody_strengthPrimary",
}, {
  category: "lowerbody_strengthPrimary",
}, {
  category: "lowerbody_strengthPrimary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "upperbody_strengthSecondary",
}, {
  category: "lowerbody_strengthSecondary",
}, {
  category: "lowerbody_strengthSecondary",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "upperbody_strengthIsolation",
}, {
  category: "lowerbody_strengthIsolation",
}, {
  category: "lowerbody_strengthIsolation",
}, {
  category: "lowerbody_strengthIsolation",
}, {
  category: "lowerbody_strengthIsolation",
}, {
  category: "mobility",
}, {
  category: "mobility",
}, {
  category: "mobility",
}, {
  category: "mobility",
}];

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
};

var newarr = [];

Object.entries(max).forEach(e => {
  newarr.push(
    fullRoutine.filter(o => o.category === e[0])
               .slice(0,e[1])
  );
});

console.log(newarr);

This is for the first small array. I still don't understand what's in the other arrays?

3 Comments

Wow. I will need to spend a few hours figuring out what you just did and how you did it haha. Thank you! As for the other arrays: the idea is that now, the remainder of items will be spread to them. The best way I can put this in words is by using a metaphor. The fullRoutine array is a bottle of water. The smaller arrays are glasses of water. I'm just trying to pour water in the glasses from the bottle until the bottle is empty and the glasses are full. The glasses have max properties per category defined in the max object. I hope that makes sense.
@YaadMohammad Ok, I'll try to add more glasses of water so no one gets thirsty.
@YaadMohammad My brain almost bursted, but I've posted a second answer that has all the small arrays.
1

May be this one should more clear:

const smallerArrays = fullRoutine.reduce((acc,elm)=>
  {
  let Nbr = (acc.filter(element=>( element.category===elm.category))).length

  if (Nbr < max[elm.category] ) { acc.push(elm) }
  return acc
  }, [] )

const fullRoutine = 
      [ { html: {x:'x'}, category: 'prehab',                      freq: 4 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 4 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 4 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 4 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 2 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 2 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 2 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 2 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 2 }
      , { html: {x:'x'}, category: 'prehab',                      freq: 2 }
      , { html: {x:'x'}, category: 'skillTechnique',              freq: 2 }
      , { html: {x:'x'}, category: 'skillTechnique',              freq: 2 }
      , { html: {x:'x'}, category: 'skillTechnique',              freq: 2 }
      , { html: {x:'x'}, category: 'skillTechnique',              freq: 2 }
      , { html: {x:'x'}, category: 'skillTechnique',              freq: 2 }
      , { html: {x:'x'}, category: 'skillTechnique',              freq: 2 }
      , { html: {x:'x'}, category: 'skillTechnique',              freq: 2 }
      , { html: {x:'x'}, category: 'skillTechnique',              freq: 2 }
      , { html: {x:'x'}, category: 'upperbody_strengthPrimary',   freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthPrimary',   freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthPrimary',   freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthPrimary',   freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthPrimary',   freq: 1 }
      , { html: {x:'x'}, category: 'lowerbody_strengthPrimary',   freq: 1 }
      , { html: {x:'x'}, category: 'lowerbody_strengthPrimary',   freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthSecondary', freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthSecondary', freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthSecondary', freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthSecondary', freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthSecondary', freq: 1 }
      , { html: {x:'x'}, category: 'lowerbody_strengthSecondary', freq: 1 }
      , { html: {x:'x'}, category: 'lowerbody_strengthSecondary', freq: 1 }
      , { html: {x:'x'}, category: 'upperbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'upperbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'upperbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'upperbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'upperbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'upperbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'upperbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'upperbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'lowerbody_strengthIsolation', freq: 1 }
      , { html: {x:'x'}, category: 'lowerbody_strengthIsolation', freq: 1 }
      , { html: {x:'x'}, category: 'lowerbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'lowerbody_strengthIsolation', freq: 2 }
      , { html: {x:'x'}, category: 'mobility',                    freq: 2 }
      , { html: {x:'x'}, category: 'mobility',                    freq: 2 }
      , { html: {x:'x'}, category: 'mobility',                    freq: 2 }
      , { html: {x:'x'}, category: 'mobility',                    freq: 2 }
      ];

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
};


const smallerArrays = fullRoutine.reduce((acc,elm)=>
  {
  let Nbr = (acc.filter(element=>( element.category===elm.category))).length

  if (Nbr < max[elm.category] ) { acc.push(elm) }
  return acc
  }, [] )


// show result :
for (let idx in smallerArrays )
  {
  console.log( idx , '=>', JSON.stringify( smallerArrays[idx] ) );
  }

2 Comments

This is exactly what I wanted! This however returns one array only. The best way I can put this in words is by using a metaphor. The fullRoutine array is a bottle of water. The smaller arrays are glasses of water. I'm just trying to pour water in the glasses from the bottle until the bottle is empty and the glasses are full. The glasses have max properties per category defined in the max object. I hope that makes sense. Thank you so much for what you've done so far!
@YaadMohammad I didn't understand your metaphor (cultural differences?) I don't see the type of real answer you are looking for at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.