配列で出現回数は取得できたのですが
data.filter(function(value){ return ( value >= 3);} );
で出現回数が3以上のものをフィルターで抽出したいのですが
上手くできずエラーになってしまいどこがおかしいのか教えていただけないでしょうか・・
html
1<ul>2<liclass="a d e b">3<inputtype="checkbox"name="check"id="label1">4<labelfor="label1"></label>5</li>6<liclass="b c a">7<inputtype="checkbox"name="check"id="label1">8<labelfor="label1"></label>9</li>10<liclass="a d e b c">11<inputtype="checkbox"name="check"id="label1">12<labelfor="label1"></label>13</li>14<liclass="d e b c">15<inputtype="checkbox"name="check"id="label1">16<labelfor="label1"></label>17</li>18</ul>19<divclass="btnA"></div>20<divclass="btnB"></div>21<divclass="btnC"></div>22<divclass="btnD"></div>
1<!DOCTYPEhtml>2<htmllang="ja">3<head>4<metacharset="UTF-8">5<title>Q200759</title>6<scriptsrc="https://code.jquery.com/jquery-3.4.1.min.js"></script>7<script>8$(function(){9$('input[name="check"]').change(function(){10$('button').hide();11if($('input[name="check"]:checked').length>=3){12var _tmp =[];13$('input[name="check"]:checked').each(function(){14 _tmp.push($(this).parents('li').attr('class').split(' '));15});1617 _tmp =Array.prototype.concat.apply([], _tmp);1819var data={};20for(var i=0;i<_tmp.length;i++){21var key=_tmp[i];22if(data[key]==undefined) data[key]=0;23 data[key]++;24}2526var showClasses =Object.entries(data).filter(([_, value])=> value >=3).map(e=> e[0]);2728$('.button').hide();29 showClasses.forEach(clazz=>{30$(`.button.${clazz}`).show();31});32};33});34});35</script>36<style>37.button{38width:80px;39height:80px;40font-size:40pt;41color:white;42text-align: center;43float: left;44margin-right:10px;45}46.button.a{background-color:red;}47.button.b{background-color:green;}48.button.c{background-color:blue;}49.button.d{background-color:orange;}50ul{list-style: none;}51li{font-size:16pt;}52label{padding-bottom:5px;}53input[type=checkbox]{transform:scale(2);vertical-align: middle;}54</style>55</head>56<body>57<ul>58<liclass="a d e b">59<inputtype="checkbox"name="check"id="checkbox1">60<labelfor="checkbox1">a d e b</label>61</li>62<liclass="b c a">63<inputtype="checkbox"name="check"id="checkbox2">64<labelfor="checkbox2">b c a</label>65</li>66<liclass="a d e b c">67<inputtype="checkbox"name="check"id="checkbox3">68<labelfor="checkbox3">a d e b c</label>69</li>70<liclass="d e b c">71<inputtype="checkbox"name="check"id="checkbox4">72<labelfor="checkbox4">d e b c</label>73</li>74</ul>75<divclass="button a">A</div>76<divclass="button b">B</div>77<divclass="button c">C</div>78<divclass="button d">D</div>79</body>80</html>
1<!DOCTYPEhtml>2<htmllang="ja">3<head>4<metacharset="UTF-8">5<title>Q200759</title>6<scriptsrc="https://code.jquery.com/jquery-3.4.1.min.js"></script>7<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/lodash.min.js"></script>8<script>9$(function(){10$('.button').hide();1112$('input[name="check"]').change(function(){13$('.button').hide();1415// チェックされているボックスを取得します。16const checkedBoxes =$('input[name="check"]:checked');1718if(checkedBoxes.length>=3){1920// _tmpは、こんな風にも作れます。21const _tmp = checkedBoxes.toArray().reduce((ary, e)=>22[...ary,...e.parentNode.classList],[])2324// _tmp に出現する異なる要素をプロパティとし、各々の個数を値とするオブジェクトは以下で得られます。25const counts = _.countBy(_tmp);2627// counts の中で個数が3以上のものだけをピックアップしたオブジェクトは以下で得られます。28const countsGte3 = _.pickBy(counts,v=> v >=3);2930// show()を適用するボタンのセレクタを作成します。31const buttonsSelectorToShow =Object.keys(countsGte3).map(x=>`.button.${x}`).join(',');3233// 上記のセレクタを使って、show()を適用します。34$(buttonsSelectorToShow).show()3536};37});38});39</script>40<style>41.button{42width:80px;43height:80px;44font-size:40pt;45color:white;46text-align: center;47float: left;48margin-right:10px;49}50.button.a{background-color:red;}51.button.b{background-color:green;}52.button.c{background-color:blue;}53.button.d{background-color:orange;}54ul{list-style: none;}55li{font-size:16pt;}56label{padding-bottom:5px;}57input[type=checkbox]{transform:scale(2);vertical-align: middle;}58</style>59</head>60<body>61<ul>62<liclass="a d e b">63<inputtype="checkbox"name="check"id="checkbox1">64<labelfor="checkbox1">a d e b</label>65</li>66<liclass="b c a">67<inputtype="checkbox"name="check"id="checkbox2">68<labelfor="checkbox2">b c a</label>69</li>70<liclass="a d e b c">71<inputtype="checkbox"name="check"id="checkbox3">72<labelfor="checkbox3">a d e b c</label>73</li>74<liclass="d e b c">75<inputtype="checkbox"name="check"id="checkbox4">76<labelfor="checkbox4">d e b c</label>77</li>78</ul>79<divclass="button a">A</div>80<divclass="button b">B</div>81<divclass="button c">C</div>82<divclass="button d">D</div>83</body>84</html>
1<script>2$(function(){3$('.btn').hide();4$('ul :checkbox').on('change',function(){5 $.each([".a",".b",".c",".d"],function(x,y){6$('.btn').filter(y).toggle($(y).has(':checked').length>=3);7});8});9});10</script>11<ul>12<li class="a d e b">13<label><input type="checkbox" name="check">adeb</label>14</li>15<li class="b c a">16<label><input type="checkbox" name="check">bca</label>17</li>18<li class="a d e b c">19<label><input type="checkbox" name="check">adebc</label>20</li>21<li class="d e b c">22<label><input type="checkbox" name="check">debc</label>23</li>24</ul>25<div class="btn a">A</div>26<div class="btn b">B</div>27<div class="btn c">C</div>28<div class="btn d">D</div>