繁体   English   中英

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

[英]Using multiple loops to check multiple conditions in javascript

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

我正在尝试执行以下操作:

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

我觉得有一种更简单的方法可以解决这个问题而不会让我头疼。 先感谢您。

编辑:我试图摆脱的第一个小数组是:

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}

使用.filter()过滤对象,使用.slice()对第一个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);

这是第一个小阵列。 我仍然不明白其他数组中有什么?

可能这个应该更清楚:

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

好的,这是我倒多杯水的第二个答案。

请注意,有空数组,因为没有更多的水可以倒。 您可以轻松过滤掉它们。

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

基本上,在倒水后,瓶子会像预期的那样失去一些水。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM