Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class='aaa' data-itemid="id001">test<span class="sum"></span></div>
<div class='aaa' data-itemid="id002">test<span class="sum"></span></div>
<div class='aaa' data-itemid="id003">test<span class="sum"></span></div>

<div class='bbb' data-itemid="id001"><span>1</span></div>
<div class='bbb' data-itemid="id002"><span>2</span><span>1</span></div>
<div class='bbb' data-itemid="id003"><span>0</span><span>0</span></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                $(function(){

  function isItemNumSum(){
    var arr = [];
    var arrNum = [];
    $('.bbb').each(function() {
      var itemId = $(this).data('itemid');

      var arrNum = $(this).find('span').map(function(){//配列に入れる
        return Number( $(this).text() );//Numberでテキストを数に変換
      }).get();

      //reduceで配列を合計しようとしたらエラーになってしまって解消の仕方がわからなかったので、下記forEachで合計した
      //var sum = arr.reduce((a,x) => a+=x);//配列から合計数を出す(エラーになってしまった)
      
      //bbbの中のspanの数を合計する処理
      var sumFn  = function(arr) {
        var sum = 0;
        arr.forEach(function(elm) {
            sum += elm;
        });
        return sum;
      };

      arr.push( {id : itemId, num : sumFn(arrNum)} );//配列に追加

    });

    for(i in arr){//bbbで作った配列をまわす
      $('.aaa').each(function(){
        if($(this).data('itemid') == arr[i].id){//同じIDの時にbbbのspanの合計値を入れる
          if(arr[i].num != '0'){
            $(this).find('.sum').text(arr[i].num);
            $(this).find('.sum').show();
          } else {
            $(this).find('.sum').hide();//0の時は非表示
          }
        }
      });
    }


    if( $('.sum').text() == '0' ){
      $('.sum').css('display','none');
    }


  }isItemNumSum();//実行

});
              
            
!
999px

Console