在javascript中使用多个循环检查多个条件

在javascript中使用多个循环检查多个条件,javascript,arrays,loops,nested-loops,Javascript,Arrays,Loops,Nested Loops,在过去的几个小时里,为了解决这个问题,我的头一直在痛。我觉得我正在以一种非常不必要的艰难方式接近它 我正在努力做到以下几点: fullRoutine是一个如下所示的数组: 0: {html: {…}, category: "prehab", freq: 4} 1: {html: {…}, category: "prehab", freq: 4} 2: {html: {…}, category: "prehab", freq: 4} 3: {html: {…}, category: "prehab

在过去的几个小时里,为了解决这个问题,我的头一直在痛。我觉得我正在以一种非常不必要的艰难方式接近它

我正在努力做到以下几点:

fullRoutine是一个如下所示的数组:

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}
这是Max对象:

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
};
这是dailyFrequency功能:

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;
}
我的想法很简单。我想将fullRoutine数组解析为多个较小的数组。max对象包含每个数组项的类别max。例如fullRoutine数组中类别为“prehab”的项在新创建的数组中只允许被解析2次。之后,必须为剩余的两个数组创建一个新数组

不要被fullRoutine数组中的freq属性弄糊涂。这与此无关。

我现在使用dailyFrequency函数来测量fullRoutine数组中的频率。我对这个问题的解决方案让我一事无成:

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]);
          }
        }
      }
    }
  }
}
使用
.filter()
对对象进行筛选,并使用
.slice()
对前n个对象进行切片

var fullRoutine=[{
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“prehab”,
}, {
类别:“技能技能”,
}, {
类别:“技能技能”,
}, {
类别:“技能技能”,
}, {
类别:“技能技能”,
}, {
类别:“技能技能”,
}, {
类别:“技能技能”,
}, {
类别:“技能技能”,
}, {
类别:“技能技能”,
}, {
类别:“上身力量第一”,
}, {
类别:“上身力量第一”,
}, {
类别:“上身力量第一”,
}, {
类别:“上身力量第一”,
}, {
类别:“上身力量第一”,
}, {
类别:“lowerbody_Strength Primary”,
}, {
类别:“lowerbody_Strength Primary”,
}, {
类别:“上半身力量第二”,
}, {
类别:“上半身力量第二”,
}, {
类别:“上半身力量第二”,
}, {
类别:“上半身力量第二”,
}, {
类别:“上半身力量第二”,
}, {
类别:“lowerbody_strengthSecondary”,
}, {
类别:“lowerbody_strengthSecondary”,
}, {
类别:“上身力量分离”,
}, {
类别:“上身力量分离”,
}, {
类别:“上身力量分离”,
}, {
类别:“上身力量分离”,
}, {
类别:“上身力量分离”,
}, {
类别:“上身力量分离”,
}, {
类别:“上身力量分离”,
}, {
类别:“上身力量分离”,
}, {
类别:“lowerbody_strengthIsolation”,
}, {
类别:“lowerbody_strengthIsolation”,
}, {
类别:“lowerbody_strengthIsolation”,
}, {
类别:“lowerbody_strengthIsolation”,
}, {
类别:“流动性”,
}, {
类别:“流动性”,
}, {
类别:“流动性”,
}, {
类别:“流动性”,
}];
常数最大={
普瑞哈布:2,
技术:2,,
上身力量主要:2,
低底座强度主要:2,
上身强度第二:2,
低底座强度第二级:2,
上身强度分离:2,
lowerbody_强度分解:2,
流动性:1
};
var newarr=[];
Object.entries(max.forEach)(e=>{
纽瓦尔推(
fullRoutine.filter(o=>o.category==e[0])
.slice(0,e[1])
);
});

控制台日志(newarr)可能是这个应该更清楚:

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 smallerrays=fullRoutine.reduce((acc,elm)=>
{
设Nbr=(acc.filter(element=>(element.category==elm.category)).length
如果(Nbr
const fullRoutine=
[{html:{x:'x'},类别:'prehab',频率:4}
,{html:{x:'x'},类别:'prehab',频率:4}
,{html:{x:'x'},类别:'prehab',频率:4}
,{html:{x:'x'},类别:'prehab',频率:4}
,{html:{x:'x'},类别:'prehab',频率:2}
,{html:{x:'x'},类别:'prehab',频率:2}
,{html:{x:'x'},类别:'prehab',频率:2}
,{html:{x:'x'},类别:'prehab',频率:2}
,{html:{x:'x'},类别:'prehab',频率:2}
,{html:{x:'x'},类别:'prehab',频率:2}
,{html:{x:'x'},类别:'skillTechnique',频率:2}
,{html:{x:'x'},类别:'skillTechnique',频率:2}
,{html:{x:'x'},类别:'skillTechnique',频率:2}
,{html:{x:'x'},类别:'skillTechnique',频率:2}
,{html:{x:'x'},类别:'skillTechnique',频率:2}
,{html:{x:'x'},类别:'skillTechnique',频率:2}
,{html:{x:'x'},类别:'skillTechnique',频率:2}
,{html:{x:'x'},类别:'skillTechnique',频率:2}
,{html:{x:'x'},类别:'upperbody_strengthPrimary',频率:1}
,{html:{x:'x'},类别:'upperbody_strengthPrimary',频率:1}
,{html:{x:'x'},类别:'upperbody_strengthPrimary',频率:1}
,{html:{x:'x'},类别:'upperbody_strengthPrimary',频率:1}
,{html:{x:'x'},类别:'upperbody_strengthPrimary',频率:1}
,{html:{x:'x'},类别:'lowerbody_strengthPrimary',频率:1}
,{html:{x:'x'},类别:'lowerbody_strengthPrimary',频率:1}
,{html:{x:'x'},类别:'upperbody_strengthSecondary',频率:1}
,{html:{x:'x'},类别:'upperbody_strengthSecondary',频率:1}
,{html:{x:'x'},类别:'upperbody_strengthSecondary',频率:1}
,{html:{x:'x'},类别:'upperbody_strengthSec
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
  }, [] )