回答編集履歴

3

テキスト修正

2019/07/16 14:47

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -256,9 +256,11 @@
256
256
 
257
257
 
258
258
 
259
+ if (checkedBoxes.length >= 3) {
260
+
259
- const _tmp = checkedBoxes.toArray().reduce((ary, e) =>
261
+ const _tmp = checkedBoxes.toArray().reduce((ary, e) =>
260
-
262
+
261
- [...ary, ...e.parentNode.classList], []);
263
+ [...ary, ...e.parentNode.classList], []);
262
264
 
263
265
  ```
264
266
 

2

テキスト修正

2019/07/16 14:47

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -318,7 +318,7 @@
318
318
 
319
319
 
320
320
 
321
- 回答のコードを上げたレポジトリで、[上記の修正](https://github.com/jun68ykt/q200759/commit/f1a798a7290c469d1ca7eecfde931ba7eb8c24cf)をした後のHTMLは以下です。
321
+ 回答のコードを上げたレポジトリで、[上記の修正](https://github.com/jun68ykt/q200759/commit/f1a798a7290c469d1ca7eecfde931ba7eb8c24cf)をした後のHTMLは以下です。(初期表示と、チェックボックスが変更されたとき、いったん全てのボタンを隠すようにしています)
322
322
 
323
323
 
324
324
 

1

テキスト修正

2019/07/16 14:45

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -234,4 +234,272 @@
234
234
 
235
235
 
236
236
 
237
+
238
+
239
+
240
+
241
+
242
+
243
+ ### 補足
244
+
245
+
246
+
247
+ 上記に挙げたHTMLに含まれるJavaScript のコードを、自分だったらさらにこのように短くしてしていくと思います、という修正案を以下に追記します。
248
+
249
+
250
+
251
+ まず、 `_tmp` は、こんな風にも作れます。
252
+
253
+ ```javascript
254
+
255
+ const checkedBoxes = $('input[name="check"]:checked');
256
+
257
+
258
+
259
+ const _tmp = checkedBoxes.toArray().reduce((ary, e) =>
260
+
261
+ [...ary, ...e.parentNode.classList], []);
262
+
263
+ ```
264
+
265
+
266
+
267
+ 次に、
268
+
269
+ - `_tmp` に出現する異なる要素をプロパティとし、各々の個数を値とするオブジェクトを作ること
270
+
271
+ - このオブジェクトのプロパティと値の組の中で、値が3以上であるもののみを持つオブジェクトを作ること
272
+
273
+
274
+
275
+ という2つのステップを行うために、[Lodash](https://lodash.com/) という便利なライブラリが提供してくれているメソッドを使います。
276
+
277
+
278
+
279
+ まず、 `_tmp` に出現する異なる要素をプロパティとし、各々の個数を値とするオブジェクトは以下で得られます。
280
+
281
+ ```javascript
282
+
283
+ const counts = _.countBy(_tmp);
284
+
285
+ ```
286
+
287
+ 上記で得られた `counts` の中で個数が3以上のものだけをピックアップしたオブジェクトは以下で得られます。
288
+
289
+ ```javascript
290
+
291
+ const countsGte3 = _.pickBy(counts, v => v >= 3);
292
+
293
+ ```
294
+
295
+ - **参考: Lodash document** [_.countBy](https://lodash.com/docs/4.17.14#countBy) , [_.pickBy](https://lodash.com/docs/4.17.14#pickBy)
296
+
297
+
298
+
299
+ 上記で得られた、 `countsGte3` を使って、 `.show()`を適用する複数のボタンを取得するセレクタを作ります。
300
+
301
+
302
+
303
+ ```javascript
304
+
305
+ const buttonsSelectorToShow = Object.keys(countsGte3).map(x => `.button.${x}`).join(',');
306
+
307
+ ```
308
+
309
+
310
+
311
+ 上記で得られた、 `buttonsSelectorToShow` を使って、以下にて、該当のボタンにまとめて `.show()`を適用できます。
312
+
313
+ ```javascript
314
+
315
+ $(buttonsSelectorToShow).show()
316
+
317
+ ```
318
+
319
+
320
+
321
+ 回答のコードを上げたレポジトリで、[上記の修正](https://github.com/jun68ykt/q200759/commit/f1a798a7290c469d1ca7eecfde931ba7eb8c24cf)をした後のHTMLは以下です。
322
+
323
+
324
+
325
+ ```html
326
+
327
+ <!DOCTYPE html>
328
+
329
+ <html lang="ja">
330
+
331
+ <head>
332
+
333
+ <meta charset="UTF-8">
334
+
335
+ <title>Q200759</title>
336
+
337
+ <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
338
+
339
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/lodash.min.js"></script>
340
+
341
+ <script>
342
+
343
+ $(function() {
344
+
345
+ $('.button').hide();
346
+
347
+
348
+
349
+ $('input[name="check"]').change(function() {
350
+
351
+ $('.button').hide();
352
+
353
+
354
+
355
+ // チェックされているボックスを取得します。
356
+
357
+ const checkedBoxes = $('input[name="check"]:checked');
358
+
359
+
360
+
361
+ if (checkedBoxes.length >= 3) {
362
+
363
+
364
+
365
+ // _tmpは、こんな風にも作れます。
366
+
367
+ const _tmp = checkedBoxes.toArray().reduce((ary, e) =>
368
+
369
+ [...ary, ...e.parentNode.classList], [])
370
+
371
+
372
+
373
+ // _tmp に出現する異なる要素をプロパティとし、各々の個数を値とするオブジェクトは以下で得られます。
374
+
375
+ const counts = _.countBy(_tmp);
376
+
377
+
378
+
379
+ // counts の中で個数が3以上のものだけをピックアップしたオブジェクトは以下で得られます。
380
+
381
+ const countsGte3 = _.pickBy(counts, v => v >= 3);
382
+
383
+
384
+
385
+ // show()を適用するボタンのセレクタを作成します。
386
+
387
+ const buttonsSelectorToShow = Object.keys(countsGte3).map(x => `.button.${x}`).join(',');
388
+
389
+
390
+
391
+ // 上記のセレクタを使って、show()を適用します。
392
+
393
+ $(buttonsSelectorToShow).show()
394
+
395
+
396
+
397
+ };
398
+
399
+ });
400
+
401
+ });
402
+
403
+ </script>
404
+
405
+ <style>
406
+
407
+ .button {
408
+
409
+ width: 80px;
410
+
411
+ height: 80px;
412
+
413
+ font-size: 40pt;
414
+
415
+ color: white;
416
+
417
+ text-align: center;
418
+
419
+ float: left;
420
+
421
+ margin-right: 10px;
422
+
423
+ }
424
+
425
+ .button.a { background-color: red; }
426
+
427
+ .button.b { background-color: green; }
428
+
429
+ .button.c { background-color: blue; }
430
+
431
+ .button.d { background-color: orange; }
432
+
433
+ ul { list-style: none; }
434
+
435
+ li { font-size: 16pt; }
436
+
437
+ label { padding-bottom: 5px; }
438
+
439
+ input[type=checkbox] { transform: scale(2); vertical-align: middle; }
440
+
441
+ </style>
442
+
443
+ </head>
444
+
445
+ <body>
446
+
447
+ <ul>
448
+
449
+ <li class="a d e b">
450
+
451
+ <input type="checkbox" name="check" id="checkbox1">
452
+
453
+ <label for="checkbox1">a d e b</label>
454
+
455
+ </li>
456
+
457
+ <li class="b c a">
458
+
459
+ <input type="checkbox" name="check" id="checkbox2">
460
+
461
+ <label for="checkbox2">b c a</label>
462
+
463
+ </li>
464
+
465
+ <li class="a d e b c">
466
+
467
+ <input type="checkbox" name="check" id="checkbox3">
468
+
469
+ <label for="checkbox3">a d e b c</label>
470
+
471
+ </li>
472
+
473
+ <li class="d e b c">
474
+
475
+ <input type="checkbox" name="check" id="checkbox4">
476
+
477
+ <label for="checkbox4">d e b c</label>
478
+
479
+ </li>
480
+
481
+ </ul>
482
+
483
+ <div class="button a">A</div>
484
+
485
+ <div class="button b">B</div>
486
+
487
+ <div class="button c">C</div>
488
+
489
+ <div class="button d">D</div>
490
+
491
+ </body>
492
+
493
+ </html>
494
+
495
+ ```
496
+
497
+ - [github.com/jun68ykt/q200759/index.html](https://github.com/jun68ykt/q200759/blob/master/index.html)
498
+
499
+
500
+
501
+ ご質問の本題からはだいぶ離れた、応用編になってしまったかもしれませんが、Lodash はちょっとずつでも使い始めてみると、とても役に立つことがあるので、そのご紹介でした。
502
+
503
+
504
+
237
505
  以上、参考になれば幸いです。